-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Clarify why a property name is required in Index Signatures #60982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
const number: unique symbol = Symbol();
interface Foo {
[number]: number;
} |
Aren’t variables named |
Having one syntax do two completely different things depending on what an identifier means is confusing at best. Imagine if you had written something like this: const foo: unique symbol = Symbol();
type foo = number;
interface Foo {
[foo]: string;
} |
Related, we tried to say "You should not use type aliases for index signatures", and people were very unhappy with that answer: #1778 (comment) |
Exactly! What’s the point of constraining a property name to an alias of |
I see your point. Personally, I find that understanding what computes a computed property often requires additional investigation beyond its name. In that sense, it might arguably be less confusing when you're in a position to trace the source of that identifier. It would be great to have an option to use a more concise syntax: The compiler will keep prioritizing the variable name of the computed property over a type identifier with the same name, as it currently does. If the type name identifier is preferred, it would still require a dummy variable under the current syntax. In all other cases, the behavior would indeed depend on what the identifier means. However, I suspect that in most scenarios, the identifier for an index signature would typically be a |
Acknowledgement
Comment
According to the TypeScript Handbook, the syntax for an index signature requires a property name, as shown in the example:
However, it seems like the following syntax could also work and might be more concise:
Could you please clarify in the documentation why a property name (like
index
in the example above) is required in index signatures? It would be helpful to understand its purpose, especially when it seems unused in the object definition.The text was updated successfully, but these errors were encountered: