Closed
Description
π Search Terms
- Ability to parametrize generic function type Ability to parametrize generic function typeΒ #44521
- Instantiation expressionsΒ #47607 instantiation expressions (see wish list comments)
- Feature Request: "extends oneof" generic constraint; allows for narrowing type parametersΒ #27808 oneof
β Viability Checklist
- 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 isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Here is an implementation for "oneof" (c.f. #27808) for an example generic function GenFunc
type GenFunc<T> = (a:T, b:T)=>T;
type OneOf_GenFunc<A extends any[], Acc extends Record<number, any>={}> = A extends [] ? Acc :
A extends [infer H, ... infer Rem] ? OneOf_GenFunc<Rem, Acc & GenFunc<H>> : never ;
type Adder = OneOf_GenFunc<[number,string]>;
// type Adder = GenFunc<number> & GenFunc<string>
declare const x:Adder;
declare const nors: number | string;
x(1,1);
x(1,""); // error today
x(nors,1); // error
// compare to
declare const genFunc: <T>(a:T, b:T)=>T;
genFunc(1,1)
genFunc(1,""); // error today
genFunc(nors,""); // no error```
However, `GenFunc` is not passed a parameter to `OneOf_GenFunc`, instead `GenFunc` must exist in the current scope, and so `OneOf_GenFunc` is not generic with respect to `GenFunc`.
If "oneof" were to be implemented as a user defined function or otherwise, then doing so via enabling generic function arguments to generic functions could kill many birds with one stone.
(*Note: The 27808 post content also talks about return type verification inside the generic function implementation body, but that is really a separate issue, and obviously isn't included here).*
### π Motivating Example
See suggestion.
### π» Use Cases
1. What do you want to use this for? A multi purpose tool that would also satisfy #27808.
2. What shortcomings exist with current approaches? No current approach.
3. What workarounds are you using in the meantime? Ad Hoc.
Metadata
Metadata
Assignees
Labels
No labels