Skip to content
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

Proposal: allow type arguments of generic being named #22631

Closed
duanyao opened this issue Mar 16, 2018 · 7 comments
Closed

Proposal: allow type arguments of generic being named #22631

duanyao opened this issue Mar 16, 2018 · 7 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@duanyao
Copy link

duanyao commented Mar 16, 2018

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).

Playground Link:
https://www.typescriptlang.org/play/#src=interface%20UserInfo%20%7B%20%7D%0D%0Avar%20usersMap%3A%20Map%3CsessionId%3A%20string%2C%20info%3A%20UserInfo%3E%3B

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.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Mar 16, 2018
@danthedaniel
Copy link

When would the name sessionId ever get used by the compiler? It seems to me that this is better left to a comment.

@yortus
Copy link
Contributor

yortus commented Mar 17, 2018

It's the same with normal function calls:

let result = 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:

// Option A:
interface UserInfo { }
var usersMap: Map</*sessionId:*/ string, /*info:*/ UserInfo>;


// Option B:
interface UserInfo { }
type MyMap<T extends {sessionId: any, info: any}, U = T['sessionId'], V = T['info']> = Map<U, V>;

let usersMap: MyMap<{sessionId: string, info: UserInfo}>;

@Airblader
Copy link

You can just do

type SessionId = string;

And then use that type.

@RyanCavanaugh RyanCavanaugh added Too Complex An issue which adding support for may be too complex for the value it adds and removed In Discussion Not yet reached consensus labels Aug 23, 2018
@RyanCavanaugh
Copy link
Member

This seems to introduce much more complexity than it produces in value.

@iheb-draouil
Copy link

iheb-draouil commented 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
Copy link

This proposal has at least two very big advantages:

  • if you edit or change something it is less risky since you won't screw your types if you pass your params in a wrong position by mistake
  • it is way more readable when you have multiple parameters

@samislam
Copy link

samislam commented Aug 4, 2024

It's the same with normal function calls:

let result = 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:

// Option A:
interface UserInfo { }
var usersMap: Map</*sessionId:*/ string, /*info:*/ UserInfo>;


// Option B:
interface UserInfo { }
type MyMap<T extends {sessionId: any, info: any}, U = T['sessionId'], V = T['info']> = Map<U, V>;

let usersMap: MyMap<{sessionId: string, info: UserInfo}>;

That's a proper JavaScript-like solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

8 participants