-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Enable the programmatic generation of overloads like feature in types. #23669
Comments
I'm not sure we need this, you could just use this definition of type Expected = <T extends All>(x: T) =>
T extends A ? Result<A> :
T extends B ? Result<B> :
any; |
Actually, I think I get what you mean, it's not possible to assign to such a type either. The problem is not creating a type that maps different types to results, the problem is assigning to it. I'll have a look if the language is currently capable of that. |
@SimonMeskens indeed, there is two issues. You seem to consider that passing from |
Not from any cursory testing no. It seems we can neither assign to the intersection or the conditional, unless there's a way I'm not seeing. |
There's a third option btw: interface Expected {
(x: A): Result<A>
(x: B): Result<B>
} We can't assign to this one either. |
That's a bunch of interesting use cases to crack! |
I think the type should be defined as: type Expected = <T extends A | B>(x: T) => T extends A ? Result<A> : Result<B>; The problem is today there is no way to implement this function without casts. we have been using |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Provided its not a duplicate of the other issue. |
I've not found anything related:
Search Terms:
overloads, programmatic
The context that explain the need to help understand the suggestion:
I'm using the io-ts (https://github.com/gcanti/io-ts) library which enables the creation of encoders/decoders by combining simpler ones.
This library can be extended by creating new types of codecs.
From a codec representation, I want to derive several generations programmatically (jsonSchema, random generators, etc..).
So I'm creating a conversion from a selected union of all base standard io-ts codecs and provides the end user of the library with a function to defines the remaining one it needs to handle for a particular codec it uses (for instance he may use a specific codec for jsJoda date in its User codec to represent its birthDate).
He may want to generate random data from that Codec but only providing the definition of it's jsJoda codec.
So the use case is a library that needs to expose a function to handles cases based on a union of types (say A | B) to represent the non standard types (codecs in the example) and requires to return specific values based on those types (
Result<A> | Result<B>
) but depending on the actual input values, like so:(x: A) => Result<A>
and(x: B) => Result<B>
and NOT like so:
(x: B) => Result<A>
or(x: A) => Result<B>
.As of today, there's no way (known to me) to derive a type
((x: A) => Result<A>) & ((x: B) => Result<B>)
fromA | B
and,given that signature
((x: A) => Result<A>) & ((x: B) => Result<B>)
, to implement it.However; the abstraction seems to exist in classes via Overloads.
Related code:
The suggestion is then to support that needs by providing both a way to encode overloads programatically (from types and not on classes) and their implementation (dispatch)
The text was updated successfully, but these errors were encountered: