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
14 changes: 12 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13785,12 +13785,22 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
&& isTypeUsableAsIndexSignature(isComputedPropertyName(node) ? checkComputedPropertyName(node) : checkExpressionCached((node as ElementAccessExpression).argumentExpression));
}

function isEntityNameExpressionOrElementAccess(node: Node): boolean {
if (isEntityNameExpression(node)) {
return true;
}
if (isElementAccessExpression(node) && isStringOrNumericLiteralLike(skipParentheses(node.argumentExpression))) {
return isEntityNameExpressionOrElementAccess(node.expression);
}
return false;
}

function isLateBindableAST(node: DeclarationName) {
if (!isComputedPropertyName(node) && !isElementAccessExpression(node)) {
return false;
}
const expr = isComputedPropertyName(node) ? node.expression : node.argumentExpression;
return isEntityNameExpression(expr);
return isEntityNameExpressionOrElementAccess(expr);
}

function isTypeUsableAsIndexSignature(type: Type): boolean {
Expand Down Expand Up @@ -52956,7 +52966,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function checkGrammarForInvalidDynamicName(node: DeclarationName, message: DiagnosticMessage) {
// Even non-bindable names are allowed as late-bound implied index signatures so long as the name is a simple `a.b.c` type name expression
if (isNonBindableDynamicName(node) && !isEntityNameExpression(isElementAccessExpression(node) ? skipParentheses(node.argumentExpression) : (node as ComputedPropertyName).expression)) {
if (isNonBindableDynamicName(node) && !isEntityNameExpressionOrElementAccess(isElementAccessExpression(node) ? skipParentheses(node.argumentExpression) : (node as ComputedPropertyName).expression)) {
return grammarErrorOnNode(node, message);
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/computedPropertyWithEnumKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [tests/cases/compiler/computedPropertyWithEnumKey.ts] ////

//// [computedPropertyWithEnumKey.ts]
enum Type { Foo = 'foo', '3x14' = '3.14' } type TypeMap = { [Type.Foo]: any; [Type['3x14']]: any; };


//// [computedPropertyWithEnumKey.js]
"use strict";
var Type;
(function (Type) {
Type["Foo"] = "foo";
Type["3x14"] = "3.14";
})(Type || (Type = {}));
16 changes: 16 additions & 0 deletions tests/baselines/reference/computedPropertyWithEnumKey.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [tests/cases/compiler/computedPropertyWithEnumKey.ts] ////

=== computedPropertyWithEnumKey.ts ===
enum Type { Foo = 'foo', '3x14' = '3.14' } type TypeMap = { [Type.Foo]: any; [Type['3x14']]: any; };
>Type : Symbol(Type, Decl(computedPropertyWithEnumKey.ts, 0, 0))
>Foo : Symbol(Type.Foo, Decl(computedPropertyWithEnumKey.ts, 0, 11))
>'3x14' : Symbol(Type['3x14'], Decl(computedPropertyWithEnumKey.ts, 0, 24))
>TypeMap : Symbol(TypeMap, Decl(computedPropertyWithEnumKey.ts, 0, 42))
>[Type.Foo] : Symbol([Type.Foo], Decl(computedPropertyWithEnumKey.ts, 0, 59))
>Type.Foo : Symbol(Type.Foo, Decl(computedPropertyWithEnumKey.ts, 0, 11))
>Type : Symbol(Type, Decl(computedPropertyWithEnumKey.ts, 0, 0))
>Foo : Symbol(Type.Foo, Decl(computedPropertyWithEnumKey.ts, 0, 11))
>[Type['3x14']] : Symbol([Type['3x14']], Decl(computedPropertyWithEnumKey.ts, 0, 76))
>Type : Symbol(Type, Decl(computedPropertyWithEnumKey.ts, 0, 0))
>'3x14' : Symbol(Type['3x14'], Decl(computedPropertyWithEnumKey.ts, 0, 24))

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

=== computedPropertyWithEnumKey.ts ===
enum Type { Foo = 'foo', '3x14' = '3.14' } type TypeMap = { [Type.Foo]: any; [Type['3x14']]: any; };
>Type : Type
> : ^^^^
>Foo : Type.Foo
> : ^^^^^^^^
>'foo' : "foo"
> : ^^^^^
>'3x14' : (typeof Type)["3x14"]
> : ^^^^^^^^^^^^^^^^^^^^^
>'3.14' : "3.14"
> : ^^^^^^
>TypeMap : TypeMap
> : ^^^^^^^
>[Type.Foo] : any
>Type.Foo : Type.Foo
> : ^^^^^^^^
>Type : typeof Type
> : ^^^^^^^^^^^
>Foo : Type.Foo
> : ^^^^^^^^
>[Type['3x14']] : any
>Type['3x14'] : (typeof Type)["3x14"]
> : ^^^^^^^^^^^^^^^^^^^^^
>Type : typeof Type
> : ^^^^^^^^^^^
>'3x14' : "3x14"
> : ^^^^^^

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
1 change: 1 addition & 0 deletions tests/cases/compiler/computedPropertyWithEnumKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum Type { Foo = 'foo', '3x14' = '3.14' } type TypeMap = { [Type.Foo]: any; [Type['3x14']]: any; };