microsoft / microsoft/TypeScript
Enum keys not accepted as computed properties if their name is not a valid identifier
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 2.9.2
Search Terms: computed property name
Code
enum Type {
Foo = 'foo',
'3x14' = '3x14'
}
type TypeMap = {
[Type.Foo]: any
[Type['3x14']]: any
}
Expected behavior:
Type['3x14'] to be usable as a computed key in a type definition.
Actual behavior:
A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type.
This is the case even for Type.Foo if it's written as Type['Foo'] instead; so it seems there is some early bailout when the bracket operator is involved. For certain key names it may not be possible to write them without resorting to brackets, like a name that starts with a digit.
Playground Link: Link
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the TypeScript 2.9.2 example in the linked Playground, then compare the accepted [Type.Foo] form with the rejected [Type['3x14']] form. Trace computed property name checking for enum member access and confirm that bracket access to a non-identifier enum member is accepted as a literal type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100