Skip to content

Conversation

@kairosci
Copy link

@kairosci kairosci commented Jan 9, 2026

Fixes #25083.

This PR relaxes the check for computed property names in type literals (specifically in isLateBindableAST) to allow ElementAccessExpression when the argument is a static literal.

Changes

Checker (src/compiler/checker.ts)

  • Updated isLateBindableAST to use a new helper isLateBindableExpression.
  • isLateBindableExpression recursively checks the expression and allows:
    • String literals (e.g., Enum['key'])
    • Numeric literals
    • Signed numeric literals (e.g., Enum[-1])
    • Parenthesized expressions (e.g., Enum[('key')])

Utilities (src/compiler/utilities.ts)

  • Updated isComputedNonLiteralName to also check for isSignedNumericLiteral
  • Updated tryGetTextOfPropertyName to handle signed numeric literals in ComputedPropertyName, aligning with getPropertyNameForPropertyNameNode

Example

enum Type {
  '3x14' = '3x14'
}
enum Numeric {
    Negative = -1,
}

type TypeMap = {
  [Type['3x14']]: any;       // Now valid
  [Numeric['Negative']]: any; // Now valid
  [Numeric[('Negative')]]: any; // Now valid
}

Verified with new test case tests/cases/compiler/enumKeysInTypeLiteral.ts covering these scenarios.

@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Jan 9, 2026
@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Jan 9, 2026
@kairosci kairosci marked this pull request as draft January 9, 2026 22:07
@kairosci kairosci force-pushed the fix/enum-computed-keys branch from c7b8d77 to 3784ace Compare January 9, 2026 22:09
@kairosci kairosci marked this pull request as ready for review January 9, 2026 22:12
@kairosci kairosci changed the title Fix(checker): Allow element access expressions in computed property names if argument is literal (Fixes #25083) fix(checker): Allow element access expressions in computed property names if argument is literal Jan 9, 2026
@kairosci kairosci force-pushed the fix/enum-computed-keys branch from 3784ace to e813bb0 Compare January 9, 2026 22:25
@kairosci kairosci force-pushed the fix/enum-computed-keys branch from 99b7bff to 91424d8 Compare January 15, 2026 16:45
Copilot AI review requested due to automatic review settings February 4, 2026 08:51
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #25083 by allowing element access expressions (e.g., Enum['key']) as computed property names in type literals when the argument is a literal. Previously, only dot notation (e.g., Enum.key) was accepted for enum members in computed property names.

Changes:

  • Modified isLateBindableAST to use a new helper isLateBindableExpression that recursively validates element access chains with literal arguments
  • Updated utility functions to handle signed numeric literals in computed property names
  • Added test case covering enum keys accessed via bracket notation in type literals

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/cases/compiler/enumKeysInTypeLiteral.ts New test case demonstrating enum element access with bracket notation in type literals
tests/baselines/reference/enumKeysInTypeLiteral.* Baseline files for the new test showing expected types, symbols, and JS output
tests/baselines/reference/isolatedDeclarationLazySymbols.* Updated baselines reflecting improved type inference for element access expressions
src/compiler/checker.ts Added isLateBindableExpression helper and modified isLateBindableAST to handle element access chains
src/compiler/utilities.ts Updated isComputedNonLiteralName and tryGetTextOfPropertyName to handle signed numeric literals; formatting improvements

Comment on lines +13752 to +13753
else if (isElementAccessExpression(node)) {
return isLateBindableExpression(node.argumentExpression);
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Enum keys not accepted as computed properties if their name is not a valid identifier

2 participants