Repository navigation
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
Open
Reuse the written name for unique-symbol keys when it resolves the same in the destination scope#65595Kaique da Silva (ktfth) wants to merge 2 commits into
Kaique da Silva (ktfth) wants to merge 2 commits into
Conversation
…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
Contributor
There was a problem hiding this comment.
🟡 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) { |
| 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Fixes #65379
Problem. When a value of type
SymbolConstructoris in scope (const S = Symbol, or a parameters: 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 withskipLibCheckthe key is lost and iterating fails with TS2488. The same happens fortoStringTagandasyncIterator, inside nested function scopes, and when the type is re-exported from another file.Cause.
trackComputedNametracks the written name's first identifier (Symbol) in the destination scope. The key itself is then reconstructed in the source scope, throughgetSymbolChain, without tracking (#64208).That reconstruction looks for a variable whose type is
SymbolConstructorto act as the namespace.Sis found first, then the candidates are sorted by declaration position, which puts the lib'sSymbolConstructorinterface ahead of it. So the printed key disagrees with what was tracked, and can even name a type.Change. For a unique-symbol key,
getReusableComputedNamereuses 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 identifiertrackComputedNametracked, 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 withnewIdentifier, 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]keepsSand itsdeclare const.Tests. New test
declarationEmitWellKnownSymbolKeyWithSymbolConstructorInScope.tscovers seven cases:toStringTagandasyncIteratorS.iteratork: 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
TandSout 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 callsgetTypeOfSymbolon each global. That scan is no longer triggered by this key, soobsBis no longer resolved early inside thexa/xbcycle. It becomes{ pa: 0; readonly ma: 1; v: 0 }, matchingobsA's{ pb: 0; readonly mb: 1; v: 0 }, instead of{ v: 0 }.dynamicNames:[s0]becomes[s2].s2is the name written in the source and is exported (typeof s0).On current
main,go test ./internal/checker/ ./internal/testrunner/passes, andgofmtandgo vet ./internal/checker/are clean.🤖 Generated with Claude Code
https://claude.ai/code/session_0135Ju6nHX6LevBTiiKb5xG7