Closed
Description
TypeScript Version: 2.7.0-dev.201xxxxx
Search Terms: named generic type parameters / arguments
Code
interface UserInfo { }
var usersMap: Map<sessionId: string, info: UserInfo>;
Expected behavior:
No error, Map<sessionId: string, info: UserInfo>
should be equivalent to Map<string, UserInfo>
.
Actual behavior:
Syntax error: Generic type 'Map<K, V>' requires 2 type argument(s).
Rationale:
Without a name, it is not often immediately clear what a type argument really mean. Take Map<string, UserInfo>
as an example, is the first argument a sessionId
, a userId
or something else? In contrast, Map<sessionId: string, info: UserInfo>
or Map<sessionId: string, UserInfo>
is very clear.
The name of a type argument doesn't affect the actual type, just like the name of argument of a function type.
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
danthedaniel commentedon Mar 17, 2018
When would the name
sessionId
ever get used by the compiler? It seems to me that this is better left to a comment.yortus commentedon Mar 17, 2018
It's the same with normal function calls:
The usual solutions are to either (a) place comments labeling each argument, or (b) pass an options object so callers have to specify key/value pairs, which is both extensible and self-documenting.
You could take both of these approaches with generics too. For example:
Airblader commentedon Jun 9, 2018
You can just do
And then use that type.
RyanCavanaugh commentedon Aug 23, 2018
This seems to introduce much more complexity than it produces in value.
iheb-draouil commentedon Aug 21, 2022
This is super useful when you have a function that takes multiple generic arguments and you only want to assert one. This is a example of what the feature could be like: sandbox
AdrianDiazCode commentedon Apr 18, 2023
This proposal has at least two very big advantages:
samislam commentedon Aug 4, 2024
That's a proper JavaScript-like solution.