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

Allow use of infer in extends clause of generic type parameter #29378

Open
5 tasks done
jscheiny opened this issue Jan 11, 2019 · 0 comments
Open
5 tasks done

Allow use of infer in extends clause of generic type parameter #29378

jscheiny opened this issue Jan 11, 2019 · 0 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

@jscheiny
Copy link

jscheiny commented Jan 11, 2019

Search Terms

generic type parameter infer keyword conditional

Suggestion

Allow the use infer keyword in the extends clause of generic type parameters. This would simply be a nice quality of life improvement to avoid unnecessary conditional types or extra free type parameters.

type ArrayType<A extends Array<infer T>> = T;

Use Cases

Currently if you have a generic type whose parameter is constrained by a generic type, you need to write a conditional to extract the nested generic type:

type ArrayType<A extends Array<any>> = A extends Array<infer T> ? T : never;

This seems unfortunate that we need to write a conditional since only the true branch will ever be satisfied. (Additionally, avoiding unnecessary usages of any would be nice.) We could avoid some duplication by dropping the constraint:

type ArrayType<A> = A extends Array<infer T> ? T : never;

But passing a non array into this type would move the error to wherever ArrayType was used (or perhaps even be silenced) instead of on the type parameter itself where the error actually occurred.

We could also do this with an extra type parameter, but this puts the onus on the user of the type to pass a correct value (and they can always just pass unknown or any):

type ArrayType<A extends Array<T>, T> = T;
type T2 = ArrayType<number[], number>;
type T3 = ArrayType<number[], unknown>;
type T1 = ArrayType<number[], any>;

Examples

type MyType<A, B> = { ... }

type OldLift<T extends MyType<any, any>, B> = T extends MyType<infer A, any>
    ? MyType<A, B>
    : never;

type NewLift<T extends MyType<infer A, any>, B> = MyType<A, B>;

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.
@weswigham weswigham added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Jan 11, 2019
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