-
Notifications
You must be signed in to change notification settings - Fork 3
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
Generics on tagged union #15
Comments
There is some prior discussion on this topic here: #12 As far as I know, this isn't possible due to limitations in TypeScript, but it's been a while, so it'd be worth checking again. |
Here's something I came up with that requires writing the Definitions object twice and requires that you manually specify the types of any import { makeTaggedUnion, MemberType, none, TaggedUnion } from "./index";
// First, make the tagged union using 'any' for all the stuff that you want to be generic:
const Maybe = makeTaggedUnion({
Some: (data: any) => data,
None: none,
});
// Then, instead of using `type Maybe = MemberType<typeof Maybe>` for the type,
// manually specify a `TaggedUnion` object as the parameter to `MemberType`, using
// generics in the appropriate places
type Maybe<T> = MemberType<
TaggedUnion<{
Some: (data: T) => T;
None: typeof none;
}>
>;
// Now, `Maybe` should work as expected. Note, however, that you have to manually
// annotate any instances of the `Maybe` type when creating them, or else they'll have
// the `any`s from the `makeTaggedUnion` call.
const data: Maybe<string> = Maybe.Some("woo");
data.match({
Some: (str) => str,
None: () => "doot",
}); |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
Thank you very much for this package. I love these Rust-like features.
I'm not an expert in TypeScript, and I was wondering if I'm just trying to do something impossible:
Is there a way in which the TypeScript compiler can infer that
value
is a number?Thank you very much in advance :)
The text was updated successfully, but these errors were encountered: