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
I have the following case where I use matchers and patterns together (apologies If I already butchered or mixed up the terms, still learning).
import{isMatching,match,P,Pattern}from"ts-pattern";interfaceFileInfo{size: number;}// I can define a patternconstbigFileSizePattern: Pattern.Pattern<FileInfo>={size: P.when((size)=>size>30000),};// I can use this pattern exportconstbigFileSizeMatcher=isMatching<Pattern.Pattern<FileInfo>>(bigFileSizePattern);bigFileSizeMatcher({size: 30001});// trueexportconstmatchFile=(file: FileInfo)=>match(file).with(bigFileSizePattern,()=>`above 30mb`).run();
What I achieved with this is I was able to define one pattern, and not duplicate it when using isMatching, or inside with().
However, when I start adding more patterns, like this:
constsmallFileSizePattern: Pattern.Pattern<FileInfo>={size: P.when((size)=>size<=30000),};constsmallFileSizeMatcher=isMatching<Pattern.Pattern<FileInfo>>(smallFileSizePattern);// And use both in my matchFile function, I get an errorsexportconstmatchFile=(file: FileInfo)=>match(file).with(bigFileSizePattern,()=>`above 30mb`,).with(smallFileSizePattern,()=>`below 30mb`).run();
I get an error always on the second with(), some infer problem that I can't decipher.
I could be doing something wrong, or what i am trying to achieve is not possible.
I appreciate any help on this 🙏
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First of all, amazing library, use it everyday.
I have the following case where I use matchers and patterns together (apologies If I already butchered or mixed up the terms, still learning).
What I achieved with this is I was able to define one pattern, and not duplicate it when using isMatching, or inside with().
However, when I start adding more patterns, like this:
I get an error always on the second with(), some infer problem that I can't decipher.
I could be doing something wrong, or what i am trying to achieve is not possible.
I appreciate any help on this 🙏
Beta Was this translation helpful? Give feedback.
All reactions