Skip to content

Commit

Permalink
Fixed accidental possibility to create Infinity/-Infinity number …
Browse files Browse the repository at this point in the history
…literal types
  • Loading branch information
Andarist committed Nov 3, 2023
1 parent 73bc0eb commit e56d3c9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/noInfinityNumberLiterals.symbols
Original file line number Diff line number Diff line change
@@ -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))

25 changes: 25 additions & 0 deletions tests/baselines/reference/noInfinityNumberLiterals.types
Original file line number Diff line number Diff line change
@@ -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

9 changes: 9 additions & 0 deletions tests/cases/compiler/noInfinityNumberLiterals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @strict: true
// @noEmit: true

type PositiveInfinity = 1e999;
type NegativeInfinity = -1e999;

const positiveInfinity = 1e999;
const positiveInfinity2 = +1e999;
const NegativeInfinity = -1e999;

0 comments on commit e56d3c9

Please sign in to comment.