Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ import {
isShorthandAmbientModuleSymbol,
isShorthandPropertyAssignment,
isSideEffectImport,
isSignedNumericLiteral,
isSingleOrDoubleQuote,
isSourceFile,
isSourceFileJS,
Expand Down Expand Up @@ -13737,14 +13738,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
&& isTypeUsableAsIndexSignature(isComputedPropertyName(node) ? checkComputedPropertyName(node) : checkExpressionCached((node as ElementAccessExpression).argumentExpression));
}

function isLateBindableAST(node: DeclarationName) {
if (!isComputedPropertyName(node) && !isElementAccessExpression(node)) {
return false;
function isLateBindableExpression(expr: Expression): boolean {
while (isElementAccessExpression(expr)) {
const argument = skipParentheses(expr.argumentExpression);
if (!isStringOrNumericLiteralLike(argument) && !isSignedNumericLiteral(argument)) return false;
Copy link

Copilot AI Feb 5, 2026

Choose a reason for hiding this comment

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

The check in the while loop is too restrictive. It should also allow EntityNameExpression (identifier or property access) as arguments, not just literals.

The current code only accepts string/numeric literals or signed numeric literals, which breaks late-bound property assignments like foo[_private] = "ok" where _private is an identifier variable.

The condition should be:

if (!isStringOrNumericLiteralLike(argument) && 
    !isSignedNumericLiteral(argument) && 
    !isEntityNameExpression(argument)) {
    return false;
}

This allows both the new use case (Enum['key'] with literal arguments) and the existing use case (foo[symbol] with identifier arguments).

Copilot uses AI. Check for mistakes.
expr = expr.expression;
}
const expr = isComputedPropertyName(node) ? node.expression : node.argumentExpression;
return isEntityNameExpression(expr);
}

function isLateBindableAST(node: DeclarationName) {
if (isComputedPropertyName(node)) {
return isLateBindableExpression(node.expression);
}
else if (isElementAccessExpression(node)) {
return isLateBindableExpression(node);
}
return false;
}

function isTypeUsableAsIndexSignature(type: Type): boolean {
return isTypeAssignableTo(type, stringNumberSymbolType);
}
Expand Down
28 changes: 18 additions & 10 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2310,7 +2310,9 @@ export function getNameFromIndexInfo(info: IndexInfo): string | undefined {

/** @internal */
export function isComputedNonLiteralName(name: PropertyName): boolean {
return name.kind === SyntaxKind.ComputedPropertyName && !isStringOrNumericLiteralLike(name.expression);
return name.kind === SyntaxKind.ComputedPropertyName &&
!isStringOrNumericLiteralLike(name.expression) &&
!isSignedNumericLiteral(name.expression);
}

/** @internal */
Expand All @@ -2326,6 +2328,12 @@ export function tryGetTextOfPropertyName(name: PropertyName | NoSubstitutionTemp
return escapeLeadingUnderscores(name.text);
case SyntaxKind.ComputedPropertyName:
if (isStringOrNumericLiteralLike(name.expression)) return escapeLeadingUnderscores(name.expression.text);
if (isSignedNumericLiteral(name.expression)) {
if (name.expression.operator === SyntaxKind.MinusToken) {
return tokenToString(name.expression.operator) + name.expression.operand.text as __String;
}
return name.expression.operand.text as __String;
}
return undefined;
case SyntaxKind.JsxNamespacedName:
return getEscapedTextOfJsxNamespacedName(name);
Expand Down Expand Up @@ -2775,7 +2783,7 @@ export function isPartOfTypeNode(node: Node): boolean {
}
// At this point, node is either a qualified name or an identifier
Debug.assert(node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName || node.kind === SyntaxKind.PropertyAccessExpression, "'node' was expected to be a qualified name, identifier or property access in 'isPartOfTypeNode'.");
// falls through
// falls through

case SyntaxKind.QualifiedName:
case SyntaxKind.PropertyAccessExpression:
Expand Down Expand Up @@ -3179,7 +3187,7 @@ export function getThisContainer(node: Node, includeArrowFunctions: boolean, inc
if (!includeArrowFunctions) {
continue;
}
// falls through
// falls through

case SyntaxKind.FunctionDeclaration:
case SyntaxKind.FunctionExpression:
Expand Down Expand Up @@ -3304,7 +3312,7 @@ export function getSuperContainer(node: Node, stopOnFunctions: boolean) {
if (!stopOnFunctions) {
continue;
}
// falls through
// falls through

case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
Expand Down Expand Up @@ -3640,7 +3648,7 @@ export function isExpressionNode(node: Node): boolean {
if (node.parent.kind === SyntaxKind.TypeQuery || isJSDocLinkLike(node.parent) || isJSDocNameReference(node.parent) || isJSDocMemberName(node.parent) || isJSXTagName(node)) {
return true;
}
// falls through
// falls through

case SyntaxKind.NumericLiteral:
case SyntaxKind.BigIntLiteral:
Expand Down Expand Up @@ -5036,7 +5044,7 @@ export function getDeclarationFromName(name: Node): Declaration | undefined {
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.NumericLiteral:
if (isComputedPropertyName(parent)) return parent.parent;
// falls through
// falls through
case SyntaxKind.Identifier:
if (isDeclaration(parent)) {
return parent.name === name ? parent : undefined;
Expand Down Expand Up @@ -5281,7 +5289,7 @@ export function getFunctionFlags(node: SignatureDeclaration | undefined): Functi
if (node.asteriskToken) {
flags |= FunctionFlags.Generator;
}
// falls through
// falls through

case SyntaxKind.ArrowFunction:
if (hasSyntacticModifier(node, ModifierFlags.Async)) {
Expand Down Expand Up @@ -8393,7 +8401,7 @@ export function getLeftmostExpression(node: Expression, stopAtCallExpressions: b
if (stopAtCallExpressions) {
return node;
}
// falls through
// falls through
case SyntaxKind.AsExpression:
case SyntaxKind.ElementAccessExpression:
case SyntaxKind.PropertyAccessExpression:
Expand Down Expand Up @@ -11551,7 +11559,7 @@ export function createNameResolver({
switch (location.kind) {
case SyntaxKind.SourceFile:
if (!isExternalOrCommonJsModule(location as SourceFile)) break;
// falls through
// falls through
case SyntaxKind.ModuleDeclaration:
const moduleExports = getSymbolOfDeclaration(location as SourceFile | ModuleDeclaration)?.exports || emptySymbols;
if (location.kind === SyntaxKind.SourceFile || (isModuleDeclaration(location) && location.flags & NodeFlags.Ambient && !isGlobalScopeAugmentation(location))) {
Expand Down Expand Up @@ -11697,7 +11705,7 @@ export function createNameResolver({
if (getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2015) {
break;
}
// falls through
// falls through
case SyntaxKind.MethodDeclaration:
case SyntaxKind.Constructor:
case SyntaxKind.GetAccessor:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
declarationEmitLateBoundAssignments.ts(4,1): error TS7053: Element implicitly has an 'any' type because expression of type 'unique symbol' can't be used to index type 'typeof foo'.
Property '[_private]' does not exist on type 'typeof foo'.
declarationEmitLateBoundAssignments.ts(6,1): error TS7053: Element implicitly has an 'any' type because expression of type '"strMemName"' can't be used to index type 'typeof foo'.
Property 'strMemName' does not exist on type 'typeof foo'.
declarationEmitLateBoundAssignments.ts(8,1): error TS7053: Element implicitly has an 'any' type because expression of type '"dashed-str-mem"' can't be used to index type 'typeof foo'.
Property 'dashed-str-mem' does not exist on type 'typeof foo'.
declarationEmitLateBoundAssignments.ts(10,1): error TS7053: Element implicitly has an 'any' type because expression of type '42' can't be used to index type 'typeof foo'.
Property '42' does not exist on type 'typeof foo'.
declarationEmitLateBoundAssignments.ts(12,19): error TS7053: Element implicitly has an 'any' type because expression of type 'unique symbol' can't be used to index type 'typeof foo'.
Property '[_private]' does not exist on type 'typeof foo'.
declarationEmitLateBoundAssignments.ts(13,19): error TS7053: Element implicitly has an 'any' type because expression of type '"strMemName"' can't be used to index type 'typeof foo'.
Property 'strMemName' does not exist on type 'typeof foo'.
declarationEmitLateBoundAssignments.ts(14,19): error TS7053: Element implicitly has an 'any' type because expression of type '42' can't be used to index type 'typeof foo'.
Property '42' does not exist on type 'typeof foo'.
declarationEmitLateBoundAssignments.ts(15,19): error TS7053: Element implicitly has an 'any' type because expression of type '"dashed-str-mem"' can't be used to index type 'typeof foo'.
Property 'dashed-str-mem' does not exist on type 'typeof foo'.


==== declarationEmitLateBoundAssignments.ts (8 errors) ====
export function foo() {}
foo.bar = 12;
const _private = Symbol();
foo[_private] = "ok";
~~~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'unique symbol' can't be used to index type 'typeof foo'.
!!! error TS7053: Property '[_private]' does not exist on type 'typeof foo'.
const strMem = "strMemName";
foo[strMem] = "ok";
~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type '"strMemName"' can't be used to index type 'typeof foo'.
!!! error TS7053: Property 'strMemName' does not exist on type 'typeof foo'.
const dashStrMem = "dashed-str-mem";
foo[dashStrMem] = "ok";
~~~~~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type '"dashed-str-mem"' can't be used to index type 'typeof foo'.
!!! error TS7053: Property 'dashed-str-mem' does not exist on type 'typeof foo'.
const numMem = 42;
foo[numMem] = "ok";
~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type '42' can't be used to index type 'typeof foo'.
!!! error TS7053: Property '42' does not exist on type 'typeof foo'.

const x: string = foo[_private];
~~~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type 'unique symbol' can't be used to index type 'typeof foo'.
!!! error TS7053: Property '[_private]' does not exist on type 'typeof foo'.
const y: string = foo[strMem];
~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type '"strMemName"' can't be used to index type 'typeof foo'.
!!! error TS7053: Property 'strMemName' does not exist on type 'typeof foo'.
const z: string = foo[numMem];
~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type '42' can't be used to index type 'typeof foo'.
!!! error TS7053: Property '42' does not exist on type 'typeof foo'.
const a: string = foo[dashStrMem];
~~~~~~~~~~~~~~~
!!! error TS7053: Element implicitly has an 'any' type because expression of type '"dashed-str-mem"' can't be used to index type 'typeof foo'.
!!! error TS7053: Property 'dashed-str-mem' does not exist on type 'typeof foo'.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ const a = foo[dashStrMem];
export declare function foo(): void;
export declare namespace foo {
var bar: number;
var strMemName: string;
}
32 changes: 16 additions & 16 deletions tests/baselines/reference/declarationEmitLateBoundAssignments.types
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const _private = Symbol();
foo[_private] = "ok";
>foo[_private] = "ok" : "ok"
> : ^^^^
>foo[_private] : string
> : ^^^^^^
>foo[_private] : any
> : ^^^
>foo : typeof foo
> : ^^^^^^^^^^
>_private : unique symbol
Expand All @@ -46,8 +46,8 @@ const strMem = "strMemName";
foo[strMem] = "ok";
>foo[strMem] = "ok" : "ok"
> : ^^^^
>foo[strMem] : string
> : ^^^^^^
>foo[strMem] : any
> : ^^^
>foo : typeof foo
> : ^^^^^^^^^^
>strMem : "strMemName"
Expand All @@ -64,8 +64,8 @@ const dashStrMem = "dashed-str-mem";
foo[dashStrMem] = "ok";
>foo[dashStrMem] = "ok" : "ok"
> : ^^^^
>foo[dashStrMem] : string
> : ^^^^^^
>foo[dashStrMem] : any
> : ^^^
>foo : typeof foo
> : ^^^^^^^^^^
>dashStrMem : "dashed-str-mem"
Expand All @@ -82,8 +82,8 @@ const numMem = 42;
foo[numMem] = "ok";
>foo[numMem] = "ok" : "ok"
> : ^^^^
>foo[numMem] : string
> : ^^^^^^
>foo[numMem] : any
> : ^^^
>foo : typeof foo
> : ^^^^^^^^^^
>numMem : 42
Expand All @@ -94,8 +94,8 @@ foo[numMem] = "ok";
const x: string = foo[_private];
>x : string
> : ^^^^^^
>foo[_private] : string
> : ^^^^^^
>foo[_private] : any
> : ^^^
>foo : typeof foo
> : ^^^^^^^^^^
>_private : unique symbol
Expand All @@ -104,8 +104,8 @@ const x: string = foo[_private];
const y: string = foo[strMem];
>y : string
> : ^^^^^^
>foo[strMem] : string
> : ^^^^^^
>foo[strMem] : any
> : ^^^
>foo : typeof foo
> : ^^^^^^^^^^
>strMem : "strMemName"
Expand All @@ -114,8 +114,8 @@ const y: string = foo[strMem];
const z: string = foo[numMem];
>z : string
> : ^^^^^^
>foo[numMem] : string
> : ^^^^^^
>foo[numMem] : any
> : ^^^
>foo : typeof foo
> : ^^^^^^^^^^
>numMem : 42
Expand All @@ -124,8 +124,8 @@ const z: string = foo[numMem];
const a: string = foo[dashStrMem];
>a : string
> : ^^^^^^
>foo[dashStrMem] : string
> : ^^^^^^
>foo[dashStrMem] : any
> : ^^^
>foo : typeof foo
> : ^^^^^^^^^^
>dashStrMem : "dashed-str-mem"
Expand Down
Loading
Loading