gvergnaud / ts-pattern

🎨 The exhaustive Pattern Matching library for TypeScript, with smart type inference.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enter multiple pipeline for objects type match

dante01yoon opened this issue · comments

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Would be good enter multiple type match

const abTest = {
  "A":  true,
  "B": true,
  "C": false,
};

match(abTest)
  .with(P.select({ A: true }), () => ...)
  .with(P.select({ B: true}), () => ...)
  .with(P._ ,() => ...)
  .exhaustive() 
// A

current match .. exhaustive only goes in single pattern matching callback, would be better use multiple conditions' callbacks

Describe the solution you'd like
Like

match(abTest)
  .with(P.select({ A: true }), () => { console.log("A")})
  .with(P.select({ B: true }), () => { console.log("B")})
  .with(P._ ,() => { console.log("C")} )
  .asMultiple()
  .exhaustive()
// A
// B

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

You should use several ifs and isMatching for that:

if (isMatching({ A: true }, abTest)) console.log("A")

if (isMatching({ B: true }, abTest)) console.log("B")

a match expression evaluates to a single return value, so we can't evaluate multiple branches otherwise it would have to return multiple values.