From e13756532ca489c166fb1d495bc271a0e01d6e6a Mon Sep 17 00:00:00 2001 From: Kaique da Silva Date: Thu, 19 Nov 2026 21:01:19 -0300 Subject: [PATCH 1/2] Reuse the written name for unique-symbol keys when it resolves the same in the destination scope With a SymbolConstructor value in scope (`const S = Symbol`, a parameter `s: SymbolConstructor`), declaration emit printed `[Symbol.iterator]` keys of inferred object types as `[SymbolConstructor.iterator]`, an interface name where a value is needed. The key was reconstructed in the source scope through the symbol chain, while trackComputedName tracked the written name's first identifier in the destination scope. Reuse the written entity name when that identifier resolves to the same symbol at the destination, so the printed key matches what was tracked; otherwise reconstruct it as before. Fixes #65379 Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7 --- tsc/internal/checker/nodebuilderimpl.go | 35 +++++++ ...wnSymbolKeyWithSymbolConstructorInScope.js | 60 ++++++++++++ ...bolKeyWithSymbolConstructorInScope.symbols | 79 ++++++++++++++++ ...ymbolKeyWithSymbolConstructorInScope.types | 94 +++++++++++++++++++ .../reference/compiler/dynamicNames.js | 2 +- .../reference/compiler/dynamicNames.types | 12 +-- ...larationEmitVariableMatchesInScopeOrder.js | 4 +- ...ationEmitVariableMatchesInScopeOrder.types | 8 +- .../uniqueSymbolsPropertyNames.types | 4 +- .../declarationComputedPropertyNames.d.ts | 2 +- ...wnSymbolKeyWithSymbolConstructorInScope.ts | 35 +++++++ 11 files changed, 320 insertions(+), 15 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js create mode 100644 tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types create mode 100644 tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index 5930d73db8b42..cde2236d6c11e 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -2587,6 +2587,9 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S return b.createPropertyNameNodeForIdentifierOrLiteral(name, singleQuote, stringNamed, isMethod, symbol) } if nameType.flags&TypeFlagsUniqueESSymbol != 0 { + if name := b.getReusableComputedName(symbol, enclosingDeclaration); name != nil { + return b.f.NewComputedPropertyName(name) + } // The reference was tracked in the destination scope by trackComputedName. // Reconstructing its spelling in the source scope must not paint that scope's declarations visible. return b.f.NewComputedPropertyName(b.symbolToExpressionWorker(nameType.AsUniqueESSymbolType().symbol, ast.SymbolFlagsValue)) @@ -2594,6 +2597,38 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S return nil } +// getReusableComputedName copies the name a late-bound property was written with, when its first identifier +// resolves to the same symbol in the destination scope. That identifier is the one trackComputedName tracked +// there, so the printed key agrees with what was made visible. Reconstructing the name in the source scope +// instead can pick a different spelling, such as a local `const S = Symbol` that is not emitted. +func (b *NodeBuilderImpl) getReusableComputedName(symbol *ast.Symbol, enclosingDeclaration *ast.Node) *ast.Node { + if enclosingDeclaration == nil || len(symbol.Declarations()) == 0 { + return nil + } + decl := symbol.Declarations()[0] + if !b.ch.hasLateBindableName(decl) { + return nil + } + var expression *ast.Node + if ast.IsBinaryExpression(decl) { + if name := ast.GetNameOfDeclaration(decl); name != nil && ast.IsElementAccessExpression(name) && ast.IsPropertyAccessEntityNameExpression(name.AsElementAccessExpression().ArgumentExpression, false /*allowJs*/) { + expression = name.AsElementAccessExpression().ArgumentExpression + } + } else if ast.IsEntityNameExpression(decl.Name().Expression()) { + expression = decl.Name().Expression() + } + if expression == nil { + return nil + } + firstIdentifier := ast.GetFirstIdentifier(expression) + meaning := ast.SymbolFlagsValue | ast.SymbolFlagsExportValue + target := b.ch.resolveName(enclosingDeclaration, firstIdentifier.Text(), meaning, nil /*nameNotFoundMessage*/, false /*isUse*/, false) + if target == nil || target != b.ch.resolveName(firstIdentifier, firstIdentifier.Text(), meaning, nil /*nameNotFoundMessage*/, false /*isUse*/, false) { + return nil + } + return b.f.DeepCloneNode(expression) +} + func (b *NodeBuilderImpl) addPropertyToElementList(propertySymbol *ast.Symbol, typeElements []*ast.TypeElement) []*ast.TypeElement { propertyIsReverseMapped := propertySymbol.CheckFlags()&ast.CheckFlagsReverseMapped != 0 var propertyType *Type diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js new file mode 100644 index 0000000000000..208f2535c8924 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js @@ -0,0 +1,60 @@ +//// [tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts] //// + +//// [variable.ts] +const S = Symbol; +export const range = { *[Symbol.iterator]() { yield 1; } }; + +//// [parameter.ts] +export function make(s: SymbolConstructor) { + return { *[Symbol.iterator]() { yield 1; } }; +} + +//// [others.ts] +const S = Symbol; +export const tagged = { [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } }; + +//// [nested.ts] +export function inner() { + const T = Symbol; + return { *[Symbol.iterator]() { yield 1; } }; +} + +//// [written.ts] +const S = Symbol; +export const written = { *[S.iterator]() { yield 1; } }; + +//// [reexport.ts] +import { range } from "./variable"; +export const again = range; + + + + + +//// [variable.d.ts] +export declare const range: { + [Symbol.iterator](): Generator; +}; +//// [parameter.d.ts] +export declare function make(s: SymbolConstructor): { + [Symbol.iterator](): Generator; +}; +//// [others.d.ts] +export declare const tagged: { + [Symbol.toStringTag]: string; + [Symbol.asyncIterator](): AsyncGenerator; +}; +//// [nested.d.ts] +export declare function inner(): { + [Symbol.iterator](): Generator; +}; +//// [written.d.ts] +declare const S: SymbolConstructor; +export declare const written: { + [S.iterator](): Generator; +}; +export {}; +//// [reexport.d.ts] +export declare const again: { + [Symbol.iterator](): Generator; +}; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols new file mode 100644 index 0000000000000..5e2a637f60c04 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols @@ -0,0 +1,79 @@ +//// [tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts] //// + +=== variable.ts === +const S = Symbol; +>S : Symbol(S, Decl(variable.ts, 0, 5)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) + +export const range = { *[Symbol.iterator]() { yield 1; } }; +>range : Symbol(range, Decl(variable.ts, 1, 12)) +>[Symbol.iterator] : Symbol([Symbol.iterator], Decl(variable.ts, 1, 22)) +>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) +>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) + +=== parameter.ts === +export function make(s: SymbolConstructor) { +>make : Symbol(make, Decl(parameter.ts, 0, 0)) +>s : Symbol(s, Decl(parameter.ts, 0, 21)) +>SymbolConstructor : Symbol(SymbolConstructor, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.asynciterable.d.ts, --, --), Decl(lib.es2020.symbol.wellknown.d.ts, --, --) ... and 2 more) + + return { *[Symbol.iterator]() { yield 1; } }; +>[Symbol.iterator] : Symbol([Symbol.iterator], Decl(parameter.ts, 1, 12)) +>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) +>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +} + +=== others.ts === +const S = Symbol; +>S : Symbol(S, Decl(others.ts, 0, 5)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) + +export const tagged = { [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } }; +>tagged : Symbol(tagged, Decl(others.ts, 1, 12)) +>[Symbol.toStringTag] : Symbol([Symbol.toStringTag], Decl(others.ts, 1, 23)) +>Symbol.toStringTag : Symbol(SymbolConstructor.toStringTag, Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) +>toStringTag : Symbol(SymbolConstructor.toStringTag, Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) +>[Symbol.asyncIterator] : Symbol([Symbol.asyncIterator], Decl(others.ts, 1, 50)) +>Symbol.asyncIterator : Symbol(SymbolConstructor.asyncIterator, Decl(lib.es2018.asynciterable.d.ts, --, --)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) +>asyncIterator : Symbol(SymbolConstructor.asyncIterator, Decl(lib.es2018.asynciterable.d.ts, --, --)) + +=== nested.ts === +export function inner() { +>inner : Symbol(inner, Decl(nested.ts, 0, 0)) + + const T = Symbol; +>T : Symbol(T, Decl(nested.ts, 1, 9)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) + + return { *[Symbol.iterator]() { yield 1; } }; +>[Symbol.iterator] : Symbol([Symbol.iterator], Decl(nested.ts, 2, 12)) +>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) +>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +} + +=== written.ts === +const S = Symbol; +>S : Symbol(S, Decl(written.ts, 0, 5)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) + +export const written = { *[S.iterator]() { yield 1; } }; +>written : Symbol(written, Decl(written.ts, 1, 12)) +>[S.iterator] : Symbol([S.iterator], Decl(written.ts, 1, 24)) +>S.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +>S : Symbol(S, Decl(written.ts, 0, 5)) +>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) + +=== reexport.ts === +import { range } from "./variable"; +>range : Symbol(range, Decl(reexport.ts, 0, 8)) + +export const again = range; +>again : Symbol(again, Decl(reexport.ts, 1, 12)) +>range : Symbol(range, Decl(reexport.ts, 0, 8)) + + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types new file mode 100644 index 0000000000000..07bf47ee92940 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types @@ -0,0 +1,94 @@ +//// [tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts] //// + +=== variable.ts === +const S = Symbol; +>S : SymbolConstructor +>Symbol : SymbolConstructor + +export const range = { *[Symbol.iterator]() { yield 1; } }; +>range : { [Symbol.iterator](): Generator; } +>{ *[Symbol.iterator]() { yield 1; } } : { [Symbol.iterator](): Generator; } +>[Symbol.iterator] : () => Generator +>Symbol.iterator : unique symbol +>Symbol : SymbolConstructor +>iterator : unique symbol +>yield 1 : any +>1 : 1 + +=== parameter.ts === +export function make(s: SymbolConstructor) { +>make : (s: SymbolConstructor) => { [Symbol.iterator](): Generator; } +>s : SymbolConstructor + + return { *[Symbol.iterator]() { yield 1; } }; +>{ *[Symbol.iterator]() { yield 1; } } : { [Symbol.iterator](): Generator; } +>[Symbol.iterator] : () => Generator +>Symbol.iterator : unique symbol +>Symbol : SymbolConstructor +>iterator : unique symbol +>yield 1 : any +>1 : 1 +} + +=== others.ts === +const S = Symbol; +>S : SymbolConstructor +>Symbol : SymbolConstructor + +export const tagged = { [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } }; +>tagged : { [Symbol.toStringTag]: string; [Symbol.asyncIterator](): AsyncGenerator; } +>{ [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } } : { [Symbol.toStringTag]: string; [Symbol.asyncIterator](): AsyncGenerator; } +>[Symbol.toStringTag] : string +>Symbol.toStringTag : unique symbol +>Symbol : SymbolConstructor +>toStringTag : unique symbol +>"x" : "x" +>[Symbol.asyncIterator] : () => AsyncGenerator +>Symbol.asyncIterator : unique symbol +>Symbol : SymbolConstructor +>asyncIterator : unique symbol +>yield 1 : any +>1 : 1 + +=== nested.ts === +export function inner() { +>inner : () => { [Symbol.iterator](): Generator; } + + const T = Symbol; +>T : SymbolConstructor +>Symbol : SymbolConstructor + + return { *[Symbol.iterator]() { yield 1; } }; +>{ *[Symbol.iterator]() { yield 1; } } : { [Symbol.iterator](): Generator; } +>[Symbol.iterator] : () => Generator +>Symbol.iterator : unique symbol +>Symbol : SymbolConstructor +>iterator : unique symbol +>yield 1 : any +>1 : 1 +} + +=== written.ts === +const S = Symbol; +>S : SymbolConstructor +>Symbol : SymbolConstructor + +export const written = { *[S.iterator]() { yield 1; } }; +>written : { [S.iterator](): Generator; } +>{ *[S.iterator]() { yield 1; } } : { [S.iterator](): Generator; } +>[S.iterator] : () => Generator +>S.iterator : unique symbol +>S : SymbolConstructor +>iterator : unique symbol +>yield 1 : any +>1 : 1 + +=== reexport.ts === +import { range } from "./variable"; +>range : { [Symbol.iterator](): Generator; } + +export const again = range; +>again : { [Symbol.iterator](): Generator; } +>range : { [Symbol.iterator](): Generator; } + + diff --git a/tsc/testdata/baselines/reference/compiler/dynamicNames.js b/tsc/testdata/baselines/reference/compiler/dynamicNames.js index 14ae09b401ba1..2585d6a86eefc 100644 --- a/tsc/testdata/baselines/reference/compiler/dynamicNames.js +++ b/tsc/testdata/baselines/reference/compiler/dynamicNames.js @@ -244,7 +244,7 @@ export declare const s2: typeof s0; export declare const o1: { a: number; 1: string; - [s0]: boolean; + [s2]: boolean; }; export declare const o1_c4: number; export declare const o1_c5: string; diff --git a/tsc/testdata/baselines/reference/compiler/dynamicNames.types b/tsc/testdata/baselines/reference/compiler/dynamicNames.types index 7e6dcf4b69390..0213b3afb9ab1 100644 --- a/tsc/testdata/baselines/reference/compiler/dynamicNames.types +++ b/tsc/testdata/baselines/reference/compiler/dynamicNames.types @@ -486,8 +486,8 @@ t0 = C; // static side // object literals export const o1 = { ->o1 : { a: number; 1: string; [s0]: boolean; } ->{ [c4]: 1, [c5]: "a", [s2]: true} : { a: number; 1: string; [s0]: boolean; } +>o1 : { a: number; 1: string; [s2]: boolean; } +>{ [c4]: 1, [c5]: "a", [s2]: true} : { a: number; 1: string; [s2]: boolean; } [c4]: 1, >[c4] : number @@ -510,24 +510,24 @@ export const o1 = { export const o1_c4 = o1[c4]; >o1_c4 : number >o1[c4] : number ->o1 : { a: number; 1: string; [s0]: boolean; } +>o1 : { a: number; 1: string; [s2]: boolean; } >c4 : "a" export const o1_c5 = o1[c5]; >o1_c5 : string >o1[c5] : string ->o1 : { a: number; 1: string; [s0]: boolean; } +>o1 : { a: number; 1: string; [s2]: boolean; } >c5 : 1 export const o1_s2 = o1[s2]; >o1_s2 : boolean >o1[s2] : boolean ->o1 : { a: number; 1: string; [s0]: boolean; } +>o1 : { a: number; 1: string; [s2]: boolean; } >s2 : unique symbol export const o2: T0 = o1; >o2 : T0 ->o1 : { a: number; 1: string; [s0]: boolean; } +>o1 : { a: number; 1: string; [s2]: boolean; } // recursive declarations // (type parameter indirection courtesy of #20400) diff --git a/tsc/testdata/baselines/reference/compiler/noFlakyDeclarationEmitVariableMatchesInScopeOrder.js b/tsc/testdata/baselines/reference/compiler/noFlakyDeclarationEmitVariableMatchesInScopeOrder.js index 30a76df171c66..bd5804d778ddc 100644 --- a/tsc/testdata/baselines/reference/compiler/noFlakyDeclarationEmitVariableMatchesInScopeOrder.js +++ b/tsc/testdata/baselines/reference/compiler/noFlakyDeclarationEmitVariableMatchesInScopeOrder.js @@ -47,7 +47,7 @@ export const obsB = expand(); //// [main.d.ts] export declare const obj: { - [Keys.key]: 1; + [keys.key]: 1; }; export declare const obsA: { pb: 0; @@ -55,5 +55,7 @@ export declare const obsA: { v: 0; }; export declare const obsB: { + pa: 0; + readonly ma: 1; v: 0; }; diff --git a/tsc/testdata/baselines/reference/compiler/noFlakyDeclarationEmitVariableMatchesInScopeOrder.types b/tsc/testdata/baselines/reference/compiler/noFlakyDeclarationEmitVariableMatchesInScopeOrder.types index 2e41e20ad32be..5e105f58d5af0 100644 --- a/tsc/testdata/baselines/reference/compiler/noFlakyDeclarationEmitVariableMatchesInScopeOrder.types +++ b/tsc/testdata/baselines/reference/compiler/noFlakyDeclarationEmitVariableMatchesInScopeOrder.types @@ -8,8 +8,8 @@ declare function expand(): { [K in keyof X]: X[K] }; >expand : () => { [K in keyof X]: X[K]; } export const obj = mk(); ->obj : { [Keys.key]: 1; } ->mk() : { [Keys.key]: 1; } +>obj : { [keys.key]: 1; } +>mk() : { [keys.key]: 1; } >mk : () => { [keys.key]: 1; } export const obsA = expand(); @@ -19,8 +19,8 @@ export const obsA = expand(); >oxa : O export const obsB = expand(); ->obsB : { v: 0; } ->expand() : { v: 0; } +>obsB : { pa: 0; readonly ma: 1; v: 0; } +>expand() : { pa: 0; readonly ma: 1; v: 0; } >expand : () => { [K in keyof X]: X[K]; } >oxb : O diff --git a/tsc/testdata/baselines/reference/conformance/uniqueSymbolsPropertyNames.types b/tsc/testdata/baselines/reference/conformance/uniqueSymbolsPropertyNames.types index f3596c9f4fda0..184d10e718297 100644 --- a/tsc/testdata/baselines/reference/conformance/uniqueSymbolsPropertyNames.types +++ b/tsc/testdata/baselines/reference/conformance/uniqueSymbolsPropertyNames.types @@ -81,8 +81,8 @@ const t0 = { }; const t1 = { ->t1 : { [OpTypes.equal]: string; } ->{ [Op.equal]: "first", [Op.equal]: "last",} : { [OpTypes.equal]: string; } +>t1 : { [Op.equal]: string; } +>{ [Op.equal]: "first", [Op.equal]: "last",} : { [Op.equal]: string; } [Op.equal]: "first", >[Op.equal] : string diff --git a/tsc/testdata/baselines/reference/transpile/declarationComputedPropertyNames.d.ts b/tsc/testdata/baselines/reference/transpile/declarationComputedPropertyNames.d.ts index c1259abf658cb..fb341b44e4f83 100644 --- a/tsc/testdata/baselines/reference/transpile/declarationComputedPropertyNames.d.ts +++ b/tsc/testdata/baselines/reference/transpile/declarationComputedPropertyNames.d.ts @@ -96,7 +96,7 @@ export declare const D: { [x: string]: number; [x: number]: number; [presentNs.a]: number; - [SymbolConstructor.toStringTag]: number; + [globalThis.Symbol.toStringTag]: number; 1: number; "2": number; }; diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts b/tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts new file mode 100644 index 0000000000000..b4da229aad0a2 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts @@ -0,0 +1,35 @@ +// @target: esnext +// @strict: true +// @declaration: true +// @emitDeclarationOnly: true + +// A value of type SymbolConstructor in scope must not turn `[Symbol.iterator]` keys into +// `[SymbolConstructor.iterator]`, which names an interface where a value is needed. + +// @filename: variable.ts +const S = Symbol; +export const range = { *[Symbol.iterator]() { yield 1; } }; + +// @filename: parameter.ts +export function make(s: SymbolConstructor) { + return { *[Symbol.iterator]() { yield 1; } }; +} + +// @filename: others.ts +const S = Symbol; +export const tagged = { [Symbol.toStringTag]: "x", async *[Symbol.asyncIterator]() { yield 1; } }; + +// @filename: nested.ts +export function inner() { + const T = Symbol; + return { *[Symbol.iterator]() { yield 1; } }; +} + +// @filename: written.ts +const S = Symbol; +export const written = { *[S.iterator]() { yield 1; } }; + +// @filename: reexport.ts +import { range } from "./variable"; +export const again = range; + From 06985fa410250f28fdd3b6f1542fec51fbdb15fb Mon Sep 17 00:00:00 2001 From: Kaique da Silva Date: Thu, 19 Nov 2026 22:02:29 -0300 Subject: [PATCH 2/2] Reuse a written key only when its declared type is the key's unique symbol Review follow-up: - A name that is a unique symbol only after control-flow narrowing (`k` declared as `typeof Symbol.iterator | undefined`) was reused, but the declaration file only has the declared type, so `[k]` did not check. Follow the declared types of the root and each property and reuse the name only when they lead to the key's unique symbol; otherwise reconstruct it as before. - Rebuild the name with newIdentifier instead of DeepCloneNode, so each identifier keeps its symbol for classified display. - Read the type of an exported variable from its export symbol. Adds the narrowed case to the test. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7 --- tsc/internal/checker/nodebuilderimpl.go | 34 ++++++++++++++++--- ...wnSymbolKeyWithSymbolConstructorInScope.js | 13 +++++++ ...bolKeyWithSymbolConstructorInScope.symbols | 19 +++++++++++ ...ymbolKeyWithSymbolConstructorInScope.types | 23 +++++++++++++ ...wnSymbolKeyWithSymbolConstructorInScope.ts | 7 ++++ 5 files changed, 92 insertions(+), 4 deletions(-) diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index cde2236d6c11e..65d50ad2f7f87 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -2587,7 +2587,7 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S return b.createPropertyNameNodeForIdentifierOrLiteral(name, singleQuote, stringNamed, isMethod, symbol) } if nameType.flags&TypeFlagsUniqueESSymbol != 0 { - if name := b.getReusableComputedName(symbol, enclosingDeclaration); name != nil { + if name := b.getReusableComputedName(symbol, nameType, enclosingDeclaration); name != nil { return b.f.NewComputedPropertyName(name) } // The reference was tracked in the destination scope by trackComputedName. @@ -2597,11 +2597,11 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S return nil } -// getReusableComputedName copies the name a late-bound property was written with, when its first identifier +// getReusableComputedName rebuilds the name a late-bound property was written with, when its first identifier // resolves to the same symbol in the destination scope. That identifier is the one trackComputedName tracked // there, so the printed key agrees with what was made visible. Reconstructing the name in the source scope // instead can pick a different spelling, such as a local `const S = Symbol` that is not emitted. -func (b *NodeBuilderImpl) getReusableComputedName(symbol *ast.Symbol, enclosingDeclaration *ast.Node) *ast.Node { +func (b *NodeBuilderImpl) getReusableComputedName(symbol *ast.Symbol, nameType *Type, enclosingDeclaration *ast.Node) *ast.Node { if enclosingDeclaration == nil || len(symbol.Declarations()) == 0 { return nil } @@ -2626,7 +2626,33 @@ func (b *NodeBuilderImpl) getReusableComputedName(symbol *ast.Symbol, enclosingD if target == nil || target != b.ch.resolveName(firstIdentifier, firstIdentifier.Text(), meaning, nil /*nameNotFoundMessage*/, false /*isUse*/, false) { return nil } - return b.f.DeepCloneNode(expression) + name, nameDeclaredType := b.rebuildEntityNameExpression(expression, target) + // Declared types, not flow-narrowed ones: the declaration file only has the declared type of each name. + if name == nil || nameDeclaredType != nameType { + return nil + } + return name +} + +// rebuildEntityNameExpression creates a copy of an entity name expression whose identifiers keep their symbols, +// and returns the declared type the copy denotes, following property declarations from the root symbol. +func (b *NodeBuilderImpl) rebuildEntityNameExpression(node *ast.Node, root *ast.Symbol) (*ast.Node, *Type) { + if ast.IsIdentifier(node) { + // An exported variable resolves to its local export-value symbol, which does not carry the type. + root = b.ch.getExportSymbolOfValueSymbolIfExported(root) + return b.newIdentifier(node.Text(), root), b.ch.getTypeOfSymbol(root) + } + access := node.AsPropertyAccessExpression() + left, leftType := b.rebuildEntityNameExpression(access.Expression, root) + if left == nil { + return nil, nil + } + memberName := access.Name().Text() + member := b.ch.getPropertyOfType(b.ch.getApparentType(leftType), memberName) + if member == nil { + return nil, nil + } + return b.f.NewPropertyAccessExpression(left, nil, b.newIdentifier(memberName, member), ast.NodeFlagsNone), b.ch.getTypeOfSymbol(member) } func (b *NodeBuilderImpl) addPropertyToElementList(propertySymbol *ast.Symbol, typeElements []*ast.TypeElement) []*ast.TypeElement { diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js index 208f2535c8924..1c787ca6dde0a 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js @@ -28,6 +28,13 @@ import { range } from "./variable"; export const again = range; +//// [narrowed.ts] +declare const k: typeof Symbol.iterator | undefined; +export function make() { + if (k === undefined) throw 0; + return { [k]: 1 }; +} + @@ -58,3 +65,9 @@ export {}; export declare const again: { [Symbol.iterator](): Generator; }; +//// [narrowed.d.ts] +declare const k: typeof Symbol.iterator | undefined; +export declare function make(): { + [Symbol.iterator]: number; +}; +export {}; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols index 5e2a637f60c04..e188a8a348fa9 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols @@ -77,3 +77,22 @@ export const again = range; >range : Symbol(range, Decl(reexport.ts, 0, 8)) +=== narrowed.ts === +declare const k: typeof Symbol.iterator | undefined; +>k : Symbol(k, Decl(narrowed.ts, 0, 13)) +>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) +>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --)) + +export function make() { +>make : Symbol(make, Decl(narrowed.ts, 0, 52)) + + if (k === undefined) throw 0; +>k : Symbol(k, Decl(narrowed.ts, 0, 13)) +>undefined : Symbol(undefined) + + return { [k]: 1 }; +>[k] : Symbol([k], Decl(narrowed.ts, 3, 12)) +>k : Symbol(k, Decl(narrowed.ts, 0, 13)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types index 07bf47ee92940..1c7bfc7f5baec 100644 --- a/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types @@ -92,3 +92,26 @@ export const again = range; >range : { [Symbol.iterator](): Generator; } +=== narrowed.ts === +declare const k: typeof Symbol.iterator | undefined; +>k : unique symbol | undefined +>Symbol.iterator : unique symbol +>Symbol : SymbolConstructor +>iterator : unique symbol + +export function make() { +>make : () => { [Symbol.iterator]: number; } + + if (k === undefined) throw 0; +>k === undefined : boolean +>k : unique symbol | undefined +>undefined : undefined +>0 : 0 + + return { [k]: 1 }; +>{ [k]: 1 } : { [Symbol.iterator]: number; } +>[k] : number +>k : unique symbol +>1 : 1 +} + diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts b/tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts index b4da229aad0a2..423bf6308988f 100644 --- a/tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts +++ b/tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts @@ -33,3 +33,10 @@ export const written = { *[S.iterator]() { yield 1; } }; import { range } from "./variable"; export const again = range; + +// @filename: narrowed.ts +declare const k: typeof Symbol.iterator | undefined; +export function make() { + if (k === undefined) throw 0; + return { [k]: 1 }; +}