Closed
Description
Suggestion
π Search Terms
- distributed conditional types
- infer keyword
β Viability 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, new syntax sugar for JS, etc.)
- This feature would agree with the rest of TypeScript's Design Goals.
β Suggestion
Doing this would allow the complete removal of an entire branch, and it's much more intuitive (the hanging infer
stumped me a whole lot when I was learning).
Before:
InferringType<T> = T extends infer U ? U extends SomeConcreteType ? U : never : never
After:
InferringType<T> = T extends SomeConcreteType infer U ? U : never
Alternatives
- Something consistent with the
as
keyword in template string literal types e.g.
InferringType<T> = T extends SomeConcreteType as U ? U : never
- No extra syntax, just smart control flow analysis on the type variable.
InferringType<T> = T extends SomeConcreteType
? T // T extends SomeConcreteType on this branch
: never // T does not extend SomeConcreteType on this branch
π Motivating Example
This playground link - has some documentation that should provide some context for the following code, but this is a pretty universal pattern anyway.
Before:
type InferEventFromTypeMatch<TypeMatch extends string> = TypeMatch extends infer InferredTypeMatch
? InferredTypeMatch extends Event['type']
? Extract<Event, AbstractEvent<InferredTypeMatch, any>>
: InferredTypeMatch extends `${infer WildcardedTypeMatch}*`
? Event extends infer EventCase
? EventCase extends { type: `${WildcardedTypeMatch}${any}` }
? EventCase
: never
: never
: never
: never
After:
type InferEventFromTypeMatch0<TypeMatchString extends string> =
TypeMatchString extends Event['type'] infer ExactTypeMatchString // Can give a much more meaningful label
? Extract<Event, AbstractEvent<ExactTypeMatchString, any>>
: InferredTypeMatch extends `${infer WildcardedTypeMatch}*`
? Event extends { type: `${WildcardedTypeMatch}${any}` } infer WildcardTypeMatch // Can give a much more meaningful label here too
? WildcardTypeMatch
: never
: never // Only two hanging nevers, rather than four