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
{{ message }}
Repository navigation
Proposal: allow type arguments of generic being named #22631
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.
letresult=doStuff('foo',true,3.14,42);// what do all these mean?
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:
This seems to introduce much more complexity than it produces in value.
Reacted by Abiria
Reacted by Ivan Kleshnin, Filip Szcześniak, Jonathan Sarrazin, Andrey Efimov, futpib, Ezzat Chamudi, Arad Alvand, TokugawaT_YD, Arman R. Tav, Velican Akcakaya and 17 more
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
Reacted by Strider, Chase Holdren, Arad Alvand, Kai Schlamp, Vincent Rubinetti, Duan Yao, henaff, Guilherme Avanci, AdrianDiazCode, Max Nanasy and 7 more
letresult=doStuff('foo',true,3.14,42);// what do all these mean?
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:
Activity
danthedaniel commentedon Mar 17, 2018
When would the name
sessionIdever 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.