From e56d3c989e5061d7b00a64c11cce0d6cf5622e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Fri, 3 Nov 2023 09:44:04 +0100 Subject: [PATCH] Fixed accidental possibility to create `Infinity`/`-Infinity` number literal types --- src/compiler/checker.ts | 3 +++ .../noInfinityNumberLiterals.symbols | 18 +++++++++++++ .../reference/noInfinityNumberLiterals.types | 25 +++++++++++++++++++ .../compiler/noInfinityNumberLiterals.ts | 9 +++++++ 4 files changed, 55 insertions(+) create mode 100644 tests/baselines/reference/noInfinityNumberLiterals.symbols create mode 100644 tests/baselines/reference/noInfinityNumberLiterals.types create mode 100644 tests/cases/compiler/noInfinityNumberLiterals.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3ac1038a4cdbe..526a59ba70755 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -37259,6 +37259,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } switch (node.operand.kind) { case SyntaxKind.NumericLiteral: + if (operandType === numberType) { + return numberType; + } switch (node.operator) { case SyntaxKind.MinusToken: return getFreshTypeOfLiteralType(getNumberLiteralType(-(node.operand as NumericLiteral).text)); diff --git a/tests/baselines/reference/noInfinityNumberLiterals.symbols b/tests/baselines/reference/noInfinityNumberLiterals.symbols new file mode 100644 index 0000000000000..2af43970817d7 --- /dev/null +++ b/tests/baselines/reference/noInfinityNumberLiterals.symbols @@ -0,0 +1,18 @@ +//// [tests/cases/compiler/noInfinityNumberLiterals.ts] //// + +=== noInfinityNumberLiterals.ts === +type PositiveInfinity = 1e999; +>PositiveInfinity : Symbol(PositiveInfinity, Decl(noInfinityNumberLiterals.ts, 0, 0)) + +type NegativeInfinity = -1e999; +>NegativeInfinity : Symbol(NegativeInfinity, Decl(noInfinityNumberLiterals.ts, 0, 30), Decl(noInfinityNumberLiterals.ts, 5, 5)) + +const positiveInfinity = 1e999; +>positiveInfinity : Symbol(positiveInfinity, Decl(noInfinityNumberLiterals.ts, 3, 5)) + +const positiveInfinity2 = +1e999; +>positiveInfinity2 : Symbol(positiveInfinity2, Decl(noInfinityNumberLiterals.ts, 4, 5)) + +const NegativeInfinity = -1e999; +>NegativeInfinity : Symbol(NegativeInfinity, Decl(noInfinityNumberLiterals.ts, 0, 30), Decl(noInfinityNumberLiterals.ts, 5, 5)) + diff --git a/tests/baselines/reference/noInfinityNumberLiterals.types b/tests/baselines/reference/noInfinityNumberLiterals.types new file mode 100644 index 0000000000000..ab0632fd5f8b5 --- /dev/null +++ b/tests/baselines/reference/noInfinityNumberLiterals.types @@ -0,0 +1,25 @@ +//// [tests/cases/compiler/noInfinityNumberLiterals.ts] //// + +=== noInfinityNumberLiterals.ts === +type PositiveInfinity = 1e999; +>PositiveInfinity : number + +type NegativeInfinity = -1e999; +>NegativeInfinity : number +>-1e999 : number +>1e999 : number + +const positiveInfinity = 1e999; +>positiveInfinity : number +>1e999 : number + +const positiveInfinity2 = +1e999; +>positiveInfinity2 : number +>+1e999 : number +>1e999 : number + +const NegativeInfinity = -1e999; +>NegativeInfinity : number +>-1e999 : number +>1e999 : number + diff --git a/tests/cases/compiler/noInfinityNumberLiterals.ts b/tests/cases/compiler/noInfinityNumberLiterals.ts new file mode 100644 index 0000000000000..0b8da1956e19a --- /dev/null +++ b/tests/cases/compiler/noInfinityNumberLiterals.ts @@ -0,0 +1,9 @@ +// @strict: true +// @noEmit: true + +type PositiveInfinity = 1e999; +type NegativeInfinity = -1e999; + +const positiveInfinity = 1e999; +const positiveInfinity2 = +1e999; +const NegativeInfinity = -1e999;