@@ -3,9 +3,11 @@ package checker
33import (
44 "github.com/microsoft/TypeScript/tsc/internal/ast"
55 "github.com/microsoft/TypeScript/tsc/internal/debug"
6+ "github.com/microsoft/TypeScript/tsc/internal/jsnum"
67 "github.com/microsoft/TypeScript/tsc/internal/nodebuilder"
78 "github.com/microsoft/TypeScript/tsc/internal/printer"
89 "github.com/microsoft/TypeScript/tsc/internal/pseudochecker"
10+ "github.com/microsoft/TypeScript/tsc/internal/scanner"
911)
1012
1113// pseudoTypeToNodeWithCheckerFallback is like pseudoTypeToNode but when the top-level pseudo type
@@ -317,13 +319,41 @@ func (b *NodeBuilderImpl) pseudoTypeToNode(t *pseudochecker.PseudoType) *ast.Nod
317319 return result
318320 case pseudochecker .PseudoTypeKindStringLiteral , pseudochecker .PseudoTypeKindNumericLiteral , pseudochecker .PseudoTypeKindBigIntLiteral :
319321 source := t .AsPseudoTypeLiteral ().Node
320- return b .f .NewLiteralTypeNode (b .reuseNode (source ))
322+ reused := b .reuseNode (source )
323+ if t .Kind == pseudochecker .PseudoTypeKindNumericLiteral {
324+ sourceLiteral := source
325+ reusedLiteral := reused
326+ if ast .IsPrefixUnaryExpression (source ) {
327+ sourceLiteral = source .AsPrefixUnaryExpression ().Operand
328+ reusedLiteral = reused .AsPrefixUnaryExpression ().Operand
329+ }
330+ if isDirectlyConstAssertedNumericLiteral (source ) {
331+ // Numeric literal text stores the rounded value, so recover the source spelling for reused const assertions.
332+ sourceFile := ast .GetSourceFileOfNode (sourceLiteral )
333+ reusedLiteral .LiteralLikeData ().Text = scanner .GetSourceTextOfNodeFromSourceFile (sourceFile , sourceLiteral , false )
334+ } else if jsnum .FromString (sourceLiteral .Text ()).IsInf () {
335+ reusedLiteral .LiteralLikeData ().Text = jsnum .InfinityLiteralText
336+ }
337+ }
338+ return b .f .NewLiteralTypeNode (reused )
321339 default :
322340 debug .AssertNever (t .Kind , "Unhandled pseudotype kind in pseudotype node construction" )
323341 return nil
324342 }
325343}
326344
345+ func isDirectlyConstAssertedNumericLiteral (node * ast.Node ) bool {
346+ for parent := node .Parent ; parent != nil ; parent = parent .Parent {
347+ if ast .IsConstAssertion (parent ) {
348+ return true
349+ }
350+ if ! ast .IsPrefixUnaryExpression (parent ) && ! ast .IsParenthesizedExpression (parent ) {
351+ return false
352+ }
353+ }
354+ return false
355+ }
356+
327357func (b * NodeBuilderImpl ) pseudoParametersToNodeList (params []* pseudochecker.PseudoParameter ) * ast.NodeList {
328358 res := make ([]* ast.Node , 0 , len (params ))
329359 for _ , p := range params {
0 commit comments