From 27075555c84b6878ccc1cfa6b206426ee40b5a8c Mon Sep 17 00:00:00 2001 From: Wenlu Wang Date: Tue, 23 Apr 2019 16:20:53 -0500 Subject: [PATCH] fix generate typenode from negative numerical literal (#30610) --- 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 9127a470325..22a658a0f7a 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3484,8 +3484,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 00000000000..0fbce596d87 --- /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 00000000000..27a039878ba --- /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; }");