From 600aaa891437be9b625afb04e12dd37676bb60a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=96=87=E7=92=90?= Date: Wed, 27 Mar 2019 15:37:59 +0800 Subject: [PATCH] fix generate typenode from negative numerical literal --- src/compiler/checker.ts | 5 +++-- .../codeFixClassImplementInterfaceWithNegativeNumber.ts | 9 +++++++++ tests/cases/fourslash/codeFixInferFromFunctionUsage.ts | 9 +++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/codeFixClassImplementInterfaceWithNegativeNumber.ts create mode 100644 tests/cases/fourslash/codeFixInferFromFunctionUsage.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index aaaacd7100762..61fe6d304890a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3360,8 +3360,9 @@ namespace ts { return createLiteralTypeNode(setEmitFlags(createLiteral((type).value), EmitFlags.NoAsciiEscaping)); } if (type.flags & TypeFlags.NumberLiteral) { - context.approximateLength += (("" + (type).value).length); - return createLiteralTypeNode((createLiteral((type).value))); + const value = (type).value; + context.approximateLength += ("" + value).length; + return createLiteralTypeNode(value < 0 ? createPrefix(SyntaxKind.MinusToken, createLiteral(-value)) : createLiteral(value)); } if (type.flags & TypeFlags.BigIntLiteral) { context.approximateLength += (pseudoBigIntToString((type).value).length) + 1; diff --git a/tests/cases/fourslash/codeFixClassImplementInterfaceWithNegativeNumber.ts b/tests/cases/fourslash/codeFixClassImplementInterfaceWithNegativeNumber.ts new file mode 100644 index 0000000000000..0fbce596d8741 --- /dev/null +++ b/tests/cases/fourslash/codeFixClassImplementInterfaceWithNegativeNumber.ts @@ -0,0 +1,9 @@ + + +/// + +//// interface X { value: -1 | 0 | 1; } +//// class Y implements X { } + +// https://github.com/Microsoft/TypeScript/issues/30431 +verify.codeFixAvailable([{ description: "Implement interface 'X'" }]); diff --git a/tests/cases/fourslash/codeFixInferFromFunctionUsage.ts b/tests/cases/fourslash/codeFixInferFromFunctionUsage.ts new file mode 100644 index 0000000000000..27a039878ba36 --- /dev/null +++ b/tests/cases/fourslash/codeFixInferFromFunctionUsage.ts @@ -0,0 +1,9 @@ +/// + +// @noImplicitAny: true +////function wrap( [| arr |] ) { +//// arr.sort(function (a: number, b: number) { return a < b ? -1 : 1 }) +//// } + +// https://github.com/Microsoft/TypeScript/issues/29330 +verify.rangeAfterCodeFix("arr: { sort: (arg0: (a: number, b: number) => 1 | -1) => void; }");