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

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

Closed
6 tasks done
craigphicks opened this issue Dec 12, 2023 · 3 comments

Comments

@craigphicks
Copy link

craigphicks commented Dec 12, 2023

πŸ” 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.
@craigphicks craigphicks changed the title Ability to pass a generic function to a generic function - (perhaps the best way to implement "oneof") Ability to pass a generic function as a parameter to a generic function - (perhaps the best way to implement "oneof") Dec 12, 2023
@fatcerberus
Copy link

Note that x(1, "") is already disallowed by the rules of generic inference; TS generally doesn't infer unions across candidates.

declare const y: <T>(a: T, b: T) => T;
y(1, "");  // this is an error today

Of course, T can still be a union if one of the candidates is a union, or if specified explicitly. What a proper oneof would do is to prevent T from ever being a union at all.

However, GenFunc is not passed [as] a parameter to OneOf_GenFunc

This sounds like you want higher-kinded types, for which there are a number of open issues; #1213 is probably the closest to canonical.

@craigphicks
Copy link
Author

It could be that 1213 is superset of this proposal, at least in terms of satisfying all the desired results describable as "Allow classes to be parametric in other parametric classes".

Conversely, proposal 1213 doesn't mention usability in conditional generic expressions which is the application described here.

Of course those differing applications should ultimately converge anyway. However, it is possible that beginning development with one target or the other would differ in what functionality became available first - i.e., that getting one working wouldn't guarantee the other worked right away without further development.

As for the way the generic function parameters are specified in 1213 - e.g., T<*,*> I think arity can be inferred from usage. More important would be

  1. Possibly indicating that the type definition is a meta-type definition
  2. Possibly Indicating that a specific parameter is a generic function
  3. Supplying default values to a generic function, which wouldn't need to be a feature in the first version anyway.

For example

type* OneOf<GenFunc><A extends any[], Acc extends Record<number, any>={}> = ...

type* rather than metatype or some other alphabetic identifier because metatype is not reserved.

Then, when

type Adder = OneOf<GenFunc><[number,string]>

is defined, the evaluation is going to be very close to OneOf_GenFunc<[number,string]> from above,
with the exception that GenFunc is "imported" into the context.

I suppose all differences from 1213 could be classified as implementation details, and therefore this really is a duplicate.

@craigphicks
Copy link
Author

craigphicks commented Dec 12, 2023

All right, I'll close this and post a comment at the end of #1213 instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants