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
import{match,select,__}from"ts-pattern";typeId={teamId: number}|{storeId: number};constselectedId: Id={teamId: 1};// Bug with wildcardconstbugWildcard=match<Id>(selectedId).with({storeId: __},()=>"storeId").with({teamId: __},()=>"teamId").exhaustive();// Return storeId instead of teamIdconsole.log("WILDCARD BUG","match result:",bugWildcard,"value to match:",selectedId);// Bug with selectconstvalue=match<Id>(selectedId).with({storeId: select()},(storeId)=>storeId).with({teamId: select()},(teamId)=>teamId).exhaustive();// Return nothingconsole.log("SELECT BUG","match result:",value,"value to match:",selectedId);// Working with __.numberconstnumber=match<Id>(selectedId).with({storeId: __.number},()=>"storeId").with({teamId: __.number},()=>"teamId").exhaustive();console.log("WORKING","match result:",number,"value to match:",selectedId);
The text was updated successfully, but these errors were encountered:
I think what's happening is that since __ and select() always match, if you try to match on a property that doesn't exist on an object, it's actually undefined which matches the pattern.
It would probably make more sense if { abc: __ } was matching only if the abc key exists on the input, though. Is this what you were expecting?
I'll have a look at the TC39 pattern matching proposal to check how they plan to handle this case
Describe the bug
Wildcard
__
andselect()
not working on property matching. As a workaround I use__.number
Code Sandbox with a minimal reproduction case
Result on console
https://codesandbox.io/s/nervous-turing-8cffw?file=/src/index.ts:0-937
Versions
Sample code
The text was updated successfully, but these errors were encountered: