diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index 5930d73db8b42..65d50ad2f7f87 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, nameType, 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,64 @@ func (b *NodeBuilderImpl) getPropertyNameNodeForSymbolFromNameType(symbol *ast.S return nil } +// 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, nameType *Type, 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 + } + 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 { 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..1c787ca6dde0a --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js @@ -0,0 +1,73 @@ +//// [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; + + +//// [narrowed.ts] +declare const k: typeof Symbol.iterator | undefined; +export function make() { + if (k === undefined) throw 0; + return { [k]: 1 }; +} + + + + +//// [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; +}; +//// [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 new file mode 100644 index 0000000000000..e188a8a348fa9 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols @@ -0,0 +1,98 @@ +//// [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)) + + +=== 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 new file mode 100644 index 0000000000000..1c7bfc7f5baec --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types @@ -0,0 +1,117 @@ +//// [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; } + + +=== 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/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..423bf6308988f --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts @@ -0,0 +1,42 @@ +// @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; + + +// @filename: narrowed.ts +declare const k: typeof Symbol.iterator | undefined; +export function make() { + if (k === undefined) throw 0; + return { [k]: 1 }; +}