⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
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 @@ -13720,14 +13721,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);
}
return false;
}

function isTypeUsableAsIndexSignature(type: Type): boolean {
return isTypeAssignableTo(type, stringNumberSymbolType);
}
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;
}