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 @@ -13735,14 +13736,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;
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.argumentExpression);
Comment on lines +13752 to +13753
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

The logic for handling ElementAccessExpression is incorrect. When node is an ElementAccessExpression (e.g., Type['key']), this code passes only node.argumentExpression (which would be the literal 'key') to isLateBindableExpression.

However, isLateBindableExpression expects to validate the full expression chain. When passed a literal like 'key', it skips the while loop (since a literal is not an ElementAccessExpression) and calls isEntityNameExpression('key'), which returns false because a string literal is not an entity name expression.

The correct approach is to pass the entire node to isLateBindableExpression, not just node.argumentExpression. This would allow the function to properly validate the element access chain and then check that the base is an entity name expression.

Copilot uses AI. Check for mistakes.
}
return false;
}

function isTypeUsableAsIndexSignature(type: Type): boolean {
return isTypeAssignableTo(type, stringNumberSymbolType);
}
Expand Down
202 changes: 105 additions & 97 deletions src/compiler/utilities.ts

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions tests/baselines/reference/enumKeysInTypeLiteral.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [tests/cases/compiler/enumKeysInTypeLiteral.ts] ////

//// [enumKeysInTypeLiteral.ts]
enum Type {
Foo = 'foo',
'3x14' = '3x14'
}

type TypeMap = {
[Type.Foo]: 1;
[Type['3x14']]: 2;
}

const t: TypeMap = {
'foo': 1,
'3x14': 2
};

enum Numeric {
Negative = -1,
Zero = 0
}

type NumericMap = {
// Valid: Accessing enum member via string literal for the name
[Numeric['Negative']]: number;
[Numeric['Zero']]: number;
// Valid: Parenthesized access
[Numeric[('Negative')]]: number;
}



//// [enumKeysInTypeLiteral.js]
var Type;
(function (Type) {
Type["Foo"] = "foo";
Type["3x14"] = "3x14";
})(Type || (Type = {}));
var t = {
'foo': 1,
'3x14': 2
};
var Numeric;
(function (Numeric) {
Numeric[Numeric["Negative"] = -1] = "Negative";
Numeric[Numeric["Zero"] = 0] = "Zero";
})(Numeric || (Numeric = {}));
71 changes: 71 additions & 0 deletions tests/baselines/reference/enumKeysInTypeLiteral.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//// [tests/cases/compiler/enumKeysInTypeLiteral.ts] ////

=== enumKeysInTypeLiteral.ts ===
enum Type {
>Type : Symbol(Type, Decl(enumKeysInTypeLiteral.ts, 0, 0))

Foo = 'foo',
>Foo : Symbol(Type.Foo, Decl(enumKeysInTypeLiteral.ts, 0, 11))

'3x14' = '3x14'
>'3x14' : Symbol(Type['3x14'], Decl(enumKeysInTypeLiteral.ts, 1, 14))
}

type TypeMap = {
>TypeMap : Symbol(TypeMap, Decl(enumKeysInTypeLiteral.ts, 3, 1))

[Type.Foo]: 1;
>[Type.Foo] : Symbol([Type.Foo], Decl(enumKeysInTypeLiteral.ts, 5, 16))
>Type.Foo : Symbol(Type.Foo, Decl(enumKeysInTypeLiteral.ts, 0, 11))
>Type : Symbol(Type, Decl(enumKeysInTypeLiteral.ts, 0, 0))
>Foo : Symbol(Type.Foo, Decl(enumKeysInTypeLiteral.ts, 0, 11))

[Type['3x14']]: 2;
>[Type['3x14']] : Symbol([Type['3x14']], Decl(enumKeysInTypeLiteral.ts, 6, 16))
>Type : Symbol(Type, Decl(enumKeysInTypeLiteral.ts, 0, 0))
>'3x14' : Symbol(Type['3x14'], Decl(enumKeysInTypeLiteral.ts, 1, 14))
}

const t: TypeMap = {
>t : Symbol(t, Decl(enumKeysInTypeLiteral.ts, 10, 5))
>TypeMap : Symbol(TypeMap, Decl(enumKeysInTypeLiteral.ts, 3, 1))

'foo': 1,
>'foo' : Symbol('foo', Decl(enumKeysInTypeLiteral.ts, 10, 20))

'3x14': 2
>'3x14' : Symbol('3x14', Decl(enumKeysInTypeLiteral.ts, 11, 13))

};

enum Numeric {
>Numeric : Symbol(Numeric, Decl(enumKeysInTypeLiteral.ts, 13, 2))

Negative = -1,
>Negative : Symbol(Numeric.Negative, Decl(enumKeysInTypeLiteral.ts, 15, 14))

Zero = 0
>Zero : Symbol(Numeric.Zero, Decl(enumKeysInTypeLiteral.ts, 16, 18))
}

type NumericMap = {
>NumericMap : Symbol(NumericMap, Decl(enumKeysInTypeLiteral.ts, 18, 1))

// Valid: Accessing enum member via string literal for the name
[Numeric['Negative']]: number;
>[Numeric['Negative']] : Symbol([Numeric['Negative']], Decl(enumKeysInTypeLiteral.ts, 20, 19), Decl(enumKeysInTypeLiteral.ts, 23, 30))
>Numeric : Symbol(Numeric, Decl(enumKeysInTypeLiteral.ts, 13, 2))
>'Negative' : Symbol(Numeric.Negative, Decl(enumKeysInTypeLiteral.ts, 15, 14))

[Numeric['Zero']]: number;
>[Numeric['Zero']] : Symbol([Numeric['Zero']], Decl(enumKeysInTypeLiteral.ts, 22, 34))
>Numeric : Symbol(Numeric, Decl(enumKeysInTypeLiteral.ts, 13, 2))
>'Zero' : Symbol(Numeric.Zero, Decl(enumKeysInTypeLiteral.ts, 16, 18))

// Valid: Parenthesized access
[Numeric[('Negative')]]: number;
>[Numeric[('Negative')]] : Symbol([Numeric['Negative']], Decl(enumKeysInTypeLiteral.ts, 20, 19), Decl(enumKeysInTypeLiteral.ts, 23, 30))
>Numeric : Symbol(Numeric, Decl(enumKeysInTypeLiteral.ts, 13, 2))
}


124 changes: 124 additions & 0 deletions tests/baselines/reference/enumKeysInTypeLiteral.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//// [tests/cases/compiler/enumKeysInTypeLiteral.ts] ////

=== enumKeysInTypeLiteral.ts ===
enum Type {
>Type : Type
> : ^^^^

Foo = 'foo',
>Foo : Type.Foo
> : ^^^^^^^^
>'foo' : "foo"
> : ^^^^^

'3x14' = '3x14'
>'3x14' : (typeof Type)["3x14"]
> : ^^^^^^^^^^^^^^^^^^^^^
>'3x14' : "3x14"
> : ^^^^^^
}

type TypeMap = {
>TypeMap : TypeMap
> : ^^^^^^^

[Type.Foo]: 1;
>[Type.Foo] : 1
> : ^
>Type.Foo : Type.Foo
> : ^^^^^^^^
>Type : typeof Type
> : ^^^^^^^^^^^
>Foo : Type.Foo
> : ^^^^^^^^

[Type['3x14']]: 2;
>[Type['3x14']] : 2
> : ^
>Type['3x14'] : (typeof Type)["3x14"]
> : ^^^^^^^^^^^^^^^^^^^^^
>Type : typeof Type
> : ^^^^^^^^^^^
>'3x14' : "3x14"
> : ^^^^^^
}

const t: TypeMap = {
>t : TypeMap
> : ^^^^^^^
>{ 'foo': 1, '3x14': 2} : { foo: 1; '3x14': 2; }
> : ^^^^^^^^^^^^^^^^^^^^^^

'foo': 1,
>'foo' : 1
> : ^
>1 : 1
> : ^

'3x14': 2
>'3x14' : 2
> : ^
>2 : 2
> : ^

};

enum Numeric {
>Numeric : Numeric
> : ^^^^^^^

Negative = -1,
>Negative : Numeric.Negative
> : ^^^^^^^^^^^^^^^^
>-1 : -1
> : ^^
>1 : 1
> : ^

Zero = 0
>Zero : Numeric.Zero
> : ^^^^^^^^^^^^
>0 : 0
> : ^
}

type NumericMap = {
>NumericMap : NumericMap
> : ^^^^^^^^^^

// Valid: Accessing enum member via string literal for the name
[Numeric['Negative']]: number;
>[Numeric['Negative']] : number
> : ^^^^^^
>Numeric['Negative'] : Numeric.Negative
> : ^^^^^^^^^^^^^^^^
>Numeric : typeof Numeric
> : ^^^^^^^^^^^^^^
>'Negative' : "Negative"
> : ^^^^^^^^^^

[Numeric['Zero']]: number;
>[Numeric['Zero']] : number
> : ^^^^^^
>Numeric['Zero'] : Numeric.Zero
> : ^^^^^^^^^^^^
>Numeric : typeof Numeric
> : ^^^^^^^^^^^^^^
>'Zero' : "Zero"
> : ^^^^^^

// Valid: Parenthesized access
[Numeric[('Negative')]]: number;
>[Numeric[('Negative')]] : number
> : ^^^^^^
>Numeric[('Negative')] : Numeric.Negative
> : ^^^^^^^^^^^^^^^^
>Numeric : typeof Numeric
> : ^^^^^^^^^^^^^^
>('Negative') : "Negative"
> : ^^^^^^^^^^
>'Negative' : "Negative"
> : ^^^^^^^^^^
}


Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
isolatedDeclarationLazySymbols.ts(1,17): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations.
isolatedDeclarationLazySymbols.ts(12,1): error TS9023: Assigning properties to functions without declaring them is not supported with --isolatedDeclarations. Add an explicit declaration for the properties assigned to this function.
isolatedDeclarationLazySymbols.ts(13,1): error TS9023: Assigning properties to functions without declaring them is not supported with --isolatedDeclarations. Add an explicit declaration for the properties assigned to this function.
isolatedDeclarationLazySymbols.ts(16,5): error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
isolatedDeclarationLazySymbols.ts(16,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationLazySymbols.ts(21,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
isolatedDeclarationLazySymbols.ts(22,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
Expand All @@ -22,15 +22,15 @@ isolatedDeclarationLazySymbols.ts(22,5): error TS9038: Computed property names o
} as const

foo[o["prop.inner"]] ="A";
~~~~~~~~~~~~~~~~~~~~
!!! error TS9023: Assigning properties to functions without declaring them is not supported with --isolatedDeclarations. Add an explicit declaration for the properties assigned to this function.
foo[o.prop.inner] = "B";
~~~~~~~~~~~~~~~~~
!!! error TS9023: Assigning properties to functions without declaring them is not supported with --isolatedDeclarations. Add an explicit declaration for the properties assigned to this function.

export class Foo {
[o["prop.inner"]] ="A"
~~~~~~~~~~~~~~~~~
!!! error TS1166: A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.
~~~~~~~~~~~~~~~~~
!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations.
[o.prop.inner] = "B"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const o = {
foo[o["prop.inner"]] ="A";
>foo[o["prop.inner"]] ="A" : "A"
> : ^^^
>foo[o["prop.inner"]] : any
> : ^^^
>foo[o["prop.inner"]] : string
> : ^^^^^^
>foo : typeof foo
> : ^^^^^^^^^^
>o["prop.inner"] : "a"
Expand Down
29 changes: 29 additions & 0 deletions tests/cases/compiler/enumKeysInTypeLiteral.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

enum Type {
Foo = 'foo',
'3x14' = '3x14'
}

type TypeMap = {
[Type.Foo]: 1;
[Type['3x14']]: 2;
}

const t: TypeMap = {
'foo': 1,
'3x14': 2
};

enum Numeric {
Negative = -1,
Zero = 0
}

type NumericMap = {
// Valid: Accessing enum member via string literal for the name
[Numeric['Negative']]: number;
[Numeric['Zero']]: number;
// Valid: Parenthesized access
[Numeric[('Negative')]]: number;
}

Loading