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

Named Generic Type Parameter #26834

Closed
4 tasks done
KSXGitHub opened this issue Sep 1, 2018 · 3 comments
Closed
4 tasks done

Named Generic Type Parameter #26834

KSXGitHub opened this issue Sep 1, 2018 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@KSXGitHub
Copy link
Contributor

KSXGitHub commented Sep 1, 2018

Search Terms

Suggestion

Allow generic consumer to pass generic argument by name, for instance:

interface Provider<Foo, Bar, Baz, Qux, Quux, ...> {
  foo: Foo
  bar: Bar
  tup: [Baz, Qux, Quux]
  ...
}

type Consumer = Provider<
  Foo = number,
  Bar = string,
  ...
>

let consumer: Provider<
  Foo = {},
  Bar = [],
  ...
>

This would allow us to write and use a generic with thousands of parameter without decreasing readability.

NOTES

  • Before you ask, this feature request is not a duplicate of Proposal: allow type arguments of generic being named #22631 in any shape or form.
  • This is similar to keyword (named) arguments in Python.
    • Vanilla JavaScript doesn't need keyword arguments as it already has objects, but there's no equivalent for TypeScript generic arguments.

Use Cases

Above

Examples

Above

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)
@svieira
Copy link

svieira commented Sep 1, 2018

Take a look at #23696 where this was tried. There was some concern that this made formerly private interfaces (the names of generic types) public. Because of this concern the PR was superseded by #26349

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 17, 2018
@RyanCavanaugh
Copy link
Member

We're still investigating the whole named type arguments story as part of partial inference and handling of generics defaults (see linked PRs as suggested).

I think we can confidently say that the "simple" case of passing type arguments by name has been mostly ruled out due to compat concerns - we have been very careful so far to make type parameter names not observable outside the declaration itself and would prefer to keep it that way unless there's absolutely no other way to accomplish the task.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants