-
Notifications
You must be signed in to change notification settings - Fork 645
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
Improve typing of the union dispatcher function #2203
Improve typing of the union dispatcher function #2203
Conversation
Thanks @thegedge! I'll take a look soon. Will you please resolve the merge conflict? On my phone right now and GitHub is not showing precisely what's in conflict. |
6a0e9ad
to
fbceb78
Compare
Ahh, I think |
Thank you! Will take a look soon! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh yeah, this is great.
Before this change, if you had the same test code, snapshot
on dispatcher would be any
, but this code gives much much better inference.
Before:
After:
And if you mess around with the union in the _brokenUnion
case, you can also see the return types. Changing line 1403
to type.string
turns the @ts-expect-error
into an error itself:
I love it.
I think for consistency's sake, we should push this out behind the upcoming v7 breaking version. It's definitely a "fix", but the issue has been around for so long (at least four years, by count of the issue) that I can see a lot of people ending up with broken types in a patch upgrade.
Thanks for this
) => Types[number] | ||
|
||
export interface UnionOptions<Types extends IAnyType[]> { | ||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: @thegedge - thank you for the extra inline documentation. Very helpful.
What does this PR do and why?
Closes #1627
Currently, the dispatcher function for union types has two issues with the typing on the dispatcher function:
This PR improves both of these by
Steps to validate locally
As always, this is all about the typechecking, so one would just have to play around with
types.union
and ensure it fails when you expect it to fail, and passes when you'd expect it to pass.I've included a test to capture two key scenarios, but let me know if there should be more!