You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Do we see any issue with the first match below, or no? Another reported issue followed up with .otherwise; but - in my case - we use .exhaustive and end up throwing this error at runtime:
Error: Pattern matching error: no pattern matches value {}
Curiously, though, there is no compile-time type error as you'd usually expect to be coming from .exhaustive.
Thinking this may be caused by using a tagged union type with a partial tag (meaning one of the unioned types has an optional tagging property). The match..with..exhaustive expression seems to be fine with { type: undefined } but then throws at runtime. Only when swapping to { type: P.optional(undefined) } does the runtime error subside; but the main issue is that .exhaustive does not seem to be checking for this edge case.
import{match,P}from'ts-pattern'// a tagged union where `type` serves as the tag but is optional for the "default" type.typeOne={// defaulttype?: 'one';};typeTwo={type: 'two';};typeThing=One|Two;constthing: Thing={};functioniThrow(thing: Thing): Thing['type']{returnmatch(thing).with({type: 'one'},()=>'one'asconst).with({type: 'two'},()=>'two'asconst)// will throw at runtime without a compile-time error.with({type: undefined},()=>undefined).exhaustive();}functionimOk(thing: Thing): Thing['type']{returnmatch(thing).with({type: 'one'},()=>'one'asconst).with({type: 'two'},()=>'two'asconst).with({type: P.optional(undefined)},()=>undefined).exhaustive();}console.log(iThrow(thing));console.log(imOk(thing));
As another data point, if we change the types like so:
To give a little more context why this may have to do with the union, this simple example does fail the .exhaustive check with a NonExhaustiveError-type error.
@gvergnaud, would you say enough detail was provided here in order to discuss this issue? No rush; but I notice a newer issue has your reply and a little time has passed. Please do keep me honest if I can give more detail.
Sorry I didn't have the time to look into this and investigate. It's most likely not an easy fix, that's why it's taking longer than other issues. Your issue was clear, thanks for providing these code examples!
Describe the bug
Do we see any issue with the first match below, or no? Another reported issue followed up with
.otherwise
; but - in my case - we use.exhaustive
and end up throwing this error at runtime:Curiously, though, there is no compile-time type error as you'd usually expect to be coming from
.exhaustive
.Thinking this may be caused by using a tagged union type with a partial tag (meaning one of the unioned types has an optional tagging property). The
match..with..exhaustive
expression seems to be fine with{ type: undefined }
but then throws at runtime. Only when swapping to{ type: P.optional(undefined) }
does the runtime error subside; but the main issue is that.exhaustive
does not seem to be checking for this edge case.As another data point, if we change the types like so:
There is an
.exhaustive
error in that case.Really appreciate this library. If this seems simple enough for a public contributor to assist with in some capacity, let me know.
Versions
The text was updated successfully, but these errors were encountered: