-
Notifications
You must be signed in to change notification settings - Fork 14
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
Union of generic variants #43
Comments
This was discussed in #4 but never realized. I don’t have time at the moment to pursue it but PRs are welcome. |
Do you have any ideas what changes are necessary to make this possible, or if it is even possible? |
In the absence of support for generics, I'm having to create the tagged union types, constructors, predicates et al. manually. In case it helps anyone else, here is an example: https://gist.github.com/OliverJAsh/9619a49d345c334e5f68473565457167 |
See my comment on #4... it seems like TypeScript needs generic value types for this to be practical. |
Hey, I was thinking on that problem a lot and finally shipped a version that Im comfortable with: https://github.com/twop/ts-union const Maybe = Union(val => ({
Nothing: of<void>(),
Just: of(val)
}));
const {Nothing, Just} = Maybe;
Just(1); // MaybeVal<number>
Just('str'); // MaybeVal<string>
Nothing<boolean>(); // MaybeVal<boolean> Conceptually I have a special token type type CreatorFuncG<K, Rec> = K extends Of<infer A>
...
? A extends [Generic]
? <P>(val: P) => UnionValG<P, Rec> : never
...
: never; Might be applicable to |
@pelotom Do you think we could work something like what ts-union has into Unionize? |
@OliverJAsh I don't have time to work on it myself, but I'm all for it! |
@twop any interest in porting your idea into unionize? |
That would be quite a challenge (unionize types a bit more complex), but might be a fun one too :) What about API? // port from ts-union
const Maybe = unionize(a => ({
Nothing: {},
Just: ofType(a) // 'Just: a' might be more ergonomic
}),
{value: "val"}); // this is probably required because not every 'a' could be spread I don't have time within the next couple of weeks but happy to try after that |
Yeah, I don't see any reason to make it more verbose than it has to be.
Hmm, yeah, I wonder if we want to enforce that they pass a |
It would be great if unionize could support union of generic variants
I'm using a generic interface in one of my projects to represent generic fetch status
and I want it to become
And I would create an union type which could be consumed by a UI component showing the fetch status according to its type.
How could we achieve that with unionize?
The text was updated successfully, but these errors were encountered: