ts-pattern-matching icon indicating copy to clipboard operation
ts-pattern-matching copied to clipboard

Possibility to filter on expressions

Open GavinRay97 opened this issue 5 years ago • 0 comments

I was wondering whether it would be possible to make a variant API that allows you to essentially leave the initial match constructor empty and to evaluate left-hand patterns. Like a more succinct case/switch:

const getActionType = (doc: DocumentApi) =>
  match<Boolean, ActionType>(doc)
  .with(isMutationAction, () => "Mutation")
  .with(isQueryAction, () => "Query")
  .otherwise(() => new Error("Neither Mutation or Query found in Document SDL"))
  .run() as ActionType

Because currently, I am doing it like this, which feels a bit awkward:

const getActionType = (doc: DocumentApi) =>
  match<Boolean, ActionType>(isMutationAction(doc))
  .with(true, () => "Mutation")
  .with(false, () => "Query")
  .otherwise(() => new Error("Neither Mutation or Query found in Document SDL"))
  .run() as ActionType

Thank you for such a wonderful library =D

GavinRay97 avatar Apr 16 '20 19:04 GavinRay97