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

Suggestion: allow infer T in type parameter constraint of type statement #39736

Open
5 tasks done
uhyo opened this issue Jul 25, 2020 · 2 comments
Open
5 tasks done

Suggestion: allow infer T in type parameter constraint of type statement #39736

uhyo opened this issue Jul 25, 2020 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@uhyo
Copy link
Contributor

uhyo commented Jul 25, 2020

Search Terms

suggestion infer extends type parameter constraint

Suggestion

Allow infer T in the right of extends of a type statement.

Examples

This suggestion allows following code:

type ReturnType<T extends (...args: any) => infer R> = R;

Which behaves similarly as:

// this code is taken from lib.es5.d.ts
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

Use Cases

As demonstrated in the example, we sometimes use conditional types with the sole purpose of use of infer T.
We have to repeat some piece of code if we want to constraint what types can be passed to ReturnType.
So this suggestion provides an abbreviated syntax for this use case.

Open questions:

  • Should union distribution occur for the new syntax?
    • My opinion is yes to align with existing conditional types
    • hmm, but then just replacing any with infer _ would change the behavior...
  • Should function type parameters also allow infer T?
    • No strong opinion

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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh
Copy link
Member

Does this let you do anything you can't already do?

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jul 29, 2020
@uhyo
Copy link
Contributor Author

uhyo commented Jul 29, 2020

No because it's an abbreviation syntax.
But reducing repeated code is important, and at least it'd help me reduce a lot of redundant code.
So more feedback is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants