You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be nice to be able to use a type alias that merely resolves to a number or a string in an index signature.
Use Cases
I sometimes create a type alias to allow me to express, more clearly, the kind of data a property holds, even when the underlying type is a simple primitive (e.g. number).
In the example, below, I want to tag any property that should hold a StateId as : StateId, even though StateId is really just a number. This makes it easier to to visually recognize what data is expected in a structure.
However, I'm not allowed to use a type alias in an index signature, even when the type alias is of the appropriate type.
Clearly, y'all have anticipated this and have given a useful error message (complete with the necessary fix) so that it will work. But, ultimately, I think it would be nice to just allow simple type aliases in this situation.
Examples
type StateId = number;
type TransMap = {
[id: StateId]: SomeOtherType, /// <--- This is the problematic line
};
This results in TS1336: An index signature parameter type cannot be a type alias. Consider writing '[id: number]: SomeOtherType' instead.
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript / JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. new expression-level syntax)
The text was updated successfully, but these errors were encountered:
Looks like a duplicate to me. I'll close this one as that one was closed. I'm, obviously, in the same camp as Yona-Appletree in that thread. Seems strange to me that I can create this alias and use it in some places but not others. From my perspective, it seems inconsistent (why allow the type alias at all if its use here seems irrelevant or misleading), and should I indeed change this underlying type I'll have to wade through a bunch of indexed types and change them from number to string or vice versa.
Search Terms
Suggestion
It would be nice to be able to use a type alias that merely resolves to a number or a string in an index signature.
Use Cases
I sometimes create a type alias to allow me to express, more clearly, the kind of data a property holds, even when the underlying type is a simple primitive (e.g.
number
).In the example, below, I want to tag any property that should hold a StateId as
: StateId
, even though StateId is really just a number. This makes it easier to to visually recognize what data is expected in a structure.However, I'm not allowed to use a type alias in an index signature, even when the type alias is of the appropriate type.
Clearly, y'all have anticipated this and have given a useful error message (complete with the necessary fix) so that it will work. But, ultimately, I think it would be nice to just allow simple type aliases in this situation.
Examples
This results in
TS1336: An index signature parameter type cannot be a type alias. Consider writing '[id: number]: SomeOtherType' instead.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: