Fix & improve match & add matchTag#8
Merged
Merged
Conversation
afd35a3 to
e737324
Compare
owi92
reviewed
Feb 19, 2025
Unfortunately, `match` had a problem where sometimes it was unsound.
This was not really the fault of `match` as TS has questionable
semantics regarding `Record<string, _>`. The following worked before:
const foo: number = match("bla", {
"a": () => 3,
});
So: no fallback, an incomplete object and still, it returns `number`.
This can of course fail at runtime. The problem is that `Record<X, _>`
works differently for `string` and for unions of strings.
The new version solves this by returning `Out | null` if not all cases
are covered, meaning that for `string` it always returns `Out | null`.
I also removed the `fallback` overload as the same can easily be
achieved via `?? fallback`.
I also added a rudimentary compile-test to make sure the problematic
cases actually cause compile errors.
e737324 to
eb327b6
Compare
owi92
approved these changes
Feb 20, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unfortunately
matchwas unsound before and could allow getting variables of typeTwhen it should have beenT | null. See the relevant commit message for more information. The changes tomatchare breaking, so this requires a major version bump.