From dd8ce2cfda1e6bd24b8566df62b5d463b13d41e5 Mon Sep 17 00:00:00 2001 From: Muhammad Faizan Date: Sun, 6 Dec 2026 02:36:06 +0500 Subject: [PATCH] Fix declaration emit for imported computed unique-symbol statics --- tsc/internal/checker/nodebuilderimpl.go | 21 +++- ...nEmitImportedComputedUniqueSymbolStatic.js | 97 +++++++++++++++++++ ...nEmitImportedComputedUniqueSymbolStatic.ts | 40 ++++++++ 3 files changed, 153 insertions(+), 5 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/declarationEmitImportedComputedUniqueSymbolStatic.js create mode 100644 tsc/testdata/tests/cases/compiler/declarationEmitImportedComputedUniqueSymbolStatic.ts diff --git a/tsc/internal/checker/nodebuilderimpl.go b/tsc/internal/checker/nodebuilderimpl.go index 83bbc5d6701c7..cecebf70a4bdd 100644 --- a/tsc/internal/checker/nodebuilderimpl.go +++ b/tsc/internal/checker/nodebuilderimpl.go @@ -805,15 +805,26 @@ func (b *NodeBuilderImpl) createAccessFromSymbolChain(chain []*ast.Symbol, index break } } - if name != nil && ast.IsComputedPropertyName(name) && ast.IsEntityName(name.Expression()) { - lhs := b.createAccessFromSymbolChain(chain, index-1, stopper, overrideTypeArguments) - if ast.IsEntityName(lhs) { + if name != nil && ast.IsComputedPropertyName(name) && ast.IsEntityNameExpression(name.Expression()) { + keyType := b.ch.checkComputedPropertyName(name) + if keyType.flags&TypeFlagsUniqueESSymbol != 0 || ast.IsEntityName(name.Expression()) { + lhs := b.createAccessFromSymbolChain(chain, index-1, stopper, overrideTypeArguments) + if !ast.IsEntityName(lhs) { + return lhs + } + var indexType *ast.TypeNode + if keyType.flags&TypeFlagsUniqueESSymbol != 0 { + // The original name belongs to the declaration's scope. Serialize + // the symbol in the destination scope so aliases are tracked. + indexType = b.symbolToTypeNode(keyType.symbol, ast.SymbolFlagsValue, nil) + } else { + indexType = b.f.NewTypeQueryNode(name.Expression(), nil) + } return b.f.NewIndexedAccessTypeNode( b.f.NewParenthesizedTypeNode(b.f.NewTypeQueryNode(lhs, nil)), - b.f.NewTypeQueryNode(name.Expression(), nil), + indexType, ) } - return lhs } symbolName = b.getNameOfSymbolAsWritten(symbol) exportName = symbol.Name() diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitImportedComputedUniqueSymbolStatic.js b/tsc/testdata/baselines/reference/compiler/declarationEmitImportedComputedUniqueSymbolStatic.js new file mode 100644 index 0000000000000..2dc30e16ad045 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitImportedComputedUniqueSymbolStatic.js @@ -0,0 +1,97 @@ +//// [tests/cases/compiler/declarationEmitImportedComputedUniqueSymbolStatic.ts] //// + +//// [statics.ts] +export declare const key: unique symbol; +export declare class K { + static readonly [key]: unique symbol; +} + +//// [main.ts] +import { K, key } from "./statics"; +export const obj = { k: K[key] } as const; + +//// [aliased.ts] +import { K, key as importedKey } from "./statics"; +const key = "unrelated"; +export const obj = { k: K[importedKey], key } as const; + +//// [qualified-statics.ts] +export declare namespace keys { + const key: unique symbol; +} +export declare class Qualified { + static readonly [keys.key]: unique symbol; +} + +//// [qualified.ts] +import { Qualified, keys as importedKeys } from "./qualified-statics"; +export const obj = { k: Qualified[importedKeys.key] } as const; + +//// [indexer.ts] +import { key } from "./statics"; +export declare function getKey(): typeof key; + +//// [import-type.ts] +import { K } from "./statics"; +import { getKey } from "./indexer"; +export const obj = { k: K[getKey()] } as const; + + +//// [statics.js] +export {}; +//// [main.js] +import { K, key } from "./statics"; +export const obj = { k: K[key] }; +//// [aliased.js] +import { K, key as importedKey } from "./statics"; +const key = "unrelated"; +export const obj = { k: K[importedKey], key }; +//// [qualified-statics.js] +export {}; +//// [qualified.js] +import { Qualified, keys as importedKeys } from "./qualified-statics"; +export const obj = { k: Qualified[importedKeys.key] }; +//// [indexer.js] +export {}; +//// [import-type.js] +import { K } from "./statics"; +import { getKey } from "./indexer"; +export const obj = { k: K[getKey()] }; + + +//// [statics.d.ts] +export declare const key: unique symbol; +export declare class K { + static readonly [key]: unique symbol; +} +//// [main.d.ts] +import { K, key } from "./statics"; +export declare const obj: { + readonly k: (typeof K)[typeof key]; +}; +//// [aliased.d.ts] +import { K, key as importedKey } from "./statics"; +export declare const obj: { + readonly k: (typeof K)[typeof importedKey]; + readonly key: "unrelated"; +}; +//// [qualified-statics.d.ts] +export declare namespace keys { + const key: unique symbol; +} +export declare class Qualified { + static readonly [keys.key]: unique symbol; +} +//// [qualified.d.ts] +import { Qualified, keys as importedKeys } from "./qualified-statics"; +export declare const obj: { + readonly k: (typeof Qualified)[typeof importedKeys.key]; +}; +//// [indexer.d.ts] +import { key } from "./statics"; +export declare function getKey(): typeof key; +//// [import-type.d.ts] +import { K } from "./statics"; +export declare const obj: { + readonly k: (typeof K)[typeof import("./statics").key]; +}; diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitImportedComputedUniqueSymbolStatic.ts b/tsc/testdata/tests/cases/compiler/declarationEmitImportedComputedUniqueSymbolStatic.ts new file mode 100644 index 0000000000000..0cf7467aecf13 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitImportedComputedUniqueSymbolStatic.ts @@ -0,0 +1,40 @@ +// @declaration: true +// @module: esnext +// @strict: true +// @noTypesAndSymbols: true + +// @filename: statics.ts +export declare const key: unique symbol; +export declare class K { + static readonly [key]: unique symbol; +} + +// @filename: main.ts +import { K, key } from "./statics"; +export const obj = { k: K[key] } as const; + +// @filename: aliased.ts +import { K, key as importedKey } from "./statics"; +const key = "unrelated"; +export const obj = { k: K[importedKey], key } as const; + +// @filename: qualified-statics.ts +export declare namespace keys { + const key: unique symbol; +} +export declare class Qualified { + static readonly [keys.key]: unique symbol; +} + +// @filename: qualified.ts +import { Qualified, keys as importedKeys } from "./qualified-statics"; +export const obj = { k: Qualified[importedKeys.key] } as const; + +// @filename: indexer.ts +import { key } from "./statics"; +export declare function getKey(): typeof key; + +// @filename: import-type.ts +import { K } from "./statics"; +import { getKey } from "./indexer"; +export const obj = { k: K[getKey()] } as const;