Skip to content

Ability to pass a generic function as a parameter to a generic function - (perhaps the best way to implement "oneof")Β #56746

Closed
@craigphicks

Description

@craigphicks

πŸ” Search Terms

βœ… Viability Checklist

⭐ 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions