Skip to content

Reuse the written name for unique-symbol keys when it resolves the same in the destination scope - #65595

Open
Kaique da Silva (ktfth) wants to merge 2 commits into
microsoft:mainfrom
ktfth:fix-65379-symbol-constructor-key
Open

Kaique da Silva (ktfth) wants to merge 2 commits into
microsoft:mainfrom
ktfth:fix-65379-symbol-constructor-key

Conversation

@ktfth

@ktfth Kaique da Silva (ktfth) commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes #65379

Problem. When a value of type SymbolConstructor is in scope (const S = Symbol, or a parameter s: SymbolConstructor), declaration emit prints well-known symbol keys of inferred object types as [SymbolConstructor.iterator]. That names an interface where a value is needed. Consumers get TS2693, and with skipLibCheck the key is lost and iterating fails with TS2488. The same happens for toStringTag and asyncIterator, inside nested function scopes, and when the type is re-exported from another file.

Cause. trackComputedName tracks the written name's first identifier (Symbol) in the destination scope. The key itself is then reconstructed in the source scope, through getSymbolChain, without tracking (#64208).

That reconstruction looks for a variable whose type is SymbolConstructor to act as the namespace. S is found first, then the candidates are sorted by declaration position, which puts the lib's SymbolConstructor interface ahead of it. So the printed key disagrees with what was tracked, and can even name a type.

Change. For a unique-symbol key, getReusableComputedName reuses the written entity name when two conditions hold. First, its first identifier must resolve to the same symbol in the destination scope; that is the identifier trackComputedName tracked, so the key and what was made visible agree. Second, the declared types along the name (root, then each property) must lead to the key's unique symbol, ignoring control-flow narrowing, because the declaration file only has declared types. The name is rebuilt with newIdentifier, so each identifier keeps its symbol. Otherwise the key is reconstructed as before. The issue's case now prints [Symbol.iterator], which the issue lists as acceptable, and a key written as [S.iterator] keeps S and its declare const.

Tests. New test declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts covers seven cases:

  • a variable
  • a parameter
  • toStringTag and asyncIterator
  • a nested scope
  • a key written as S.iterator
  • a re-export from another file
  • a key that is a unique symbol only after narrowing (k: typeof Symbol.iterator | undefined), which falls back to [Symbol.iterator]

Without the change, its declaration files have seven TS2693 errors. 6.0.3 also fails the nested and re-export cases, with T and S out of scope. With the change, every declaration file compiles.

Four existing baselines change. In three of them the old output had this bug, printing an interface where a value is needed:

  • transpile/declarationComputedPropertyNames.d.ts: [SymbolConstructor.toStringTag] becomes [globalThis.Symbol.toStringTag], as written.
  • uniqueSymbolsPropertyNames.types: [OpTypes.equal] (an interface) becomes [Op.equal].
  • noFlakyDeclarationEmitVariableMatchesInScopeOrder: [Keys.key] (an interface) becomes [keys.key]. The key no longer needs the reconstruction, whose scan of in-scope variables calls getTypeOfSymbol on each global. That scan is no longer triggered by this key, so obsB is no longer resolved early inside the xa/xb cycle. It becomes { pa: 0; readonly ma: 1; v: 0 }, matching obsA's { pb: 0; readonly mb: 1; v: 0 }, instead of { v: 0 }.
  • dynamicNames: [s0] becomes [s2]. s2 is the name written in the source and is exported (typeof s0).

On current main, go test ./internal/checker/ ./internal/testrunner/ passes, and gofmt and go vet ./internal/checker/ are clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7

…me 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 microsoft#65379

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7
Copilot AI balanced review requested due to automatic review settings November 20, 2026 00:02
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 20, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Symbol-name reuse can emit invalid declarations when uniqueness depends on flow narrowing, while cloned identifiers lose associations needed for hover classification.

2 open issues
What changed in this PR

This PR addresses #65379 by preserving written unique-symbol keys during type serialization when their root identifier resolves to the same symbol in the destination scope. This avoids declaration output that incorrectly uses interface names as values.

Changes:

  • Adds computed-name reuse with the existing reconstruction path as a fallback.
  • Adds six regression scenarios covering aliases, parameters, nested scopes, other well-known symbols, and cross-file reuse.
  • Updates declaration and type baselines for the resulting names and resolution-order changes.
File Description
tsc/​internal/​checker/​nodebuilderimpl.go Adds destination-scope checks and computed-name reuse.
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.ts Adds six declaration-emit regression scenarios.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.js Records expected declarations for the new scenarios.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.symbols Records symbol bindings for the new scenarios.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.types Records inferred types for the new scenarios.
tsc/​testdata/​baselines/​reference/​transpile/​declarationComputedPropertyNames.d.ts Preserves the written globalThis.Symbol.toStringTag key.
tsc/​testdata/​baselines/​reference/​conformance/​uniqueSymbolsPropertyNames.types Uses the value name Op.equal.
tsc/​testdata/​baselines/​reference/​compiler/​noFlakyDeclarationEmitVariableMatchesInScopeOrder.types Updates key spelling and cyclic-resolution type results.
tsc/​testdata/​baselines/​reference/​compiler/​noFlakyDeclarationEmitVariableMatchesInScopeOrder.js Records corrected key spelling and fuller obsB declarations.
tsc/​testdata/​baselines/​reference/​compiler/​dynamicNames.types Preserves the written s2 key in inferred types.
tsc/​testdata/​baselines/​reference/​compiler/​dynamicNames.js Preserves the exported s2 key in declarations.

Review effort: Balanced

Comment on lines +2625 to +2626
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) {
Comment thread tsc/internal/checker/nodebuilderimpl.go Outdated
if target == nil || target != b.ch.resolveName(firstIdentifier, firstIdentifier.Text(), meaning, nil /*nameNotFoundMessage*/, false /*isUse*/, false) {
return nil
}
return b.f.DeepCloneNode(expression)
…ymbol

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) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Declaration emit writes [SymbolConstructor.iterator] as a computed key when a SymbolConstructor value is in scope

2 participants