Skip to content

Help me simplify this nested ternary expression with ts-pattern #216

Answered by darrylnoakes
harshmandan asked this question in Q&A
Discussion options

You must be logged in to vote

If you really want to, you can use tuples to perform nested matching. However, TS Pattern can struggle with the relations; a value may not be relevant if another value is not true, but TS Pattern will want you to check that combination anyway. Effectively, you are abusing parallel matching for testing nested conditions.

For example, I believe this should have the same behavior as your original code:

function getFilteredProperties(properties: FilterProperty[]) {
  return properties.filter((property) =>
    match([addFilterAction, activeFilter, property])
      .with([false, { share: true }, "Date"], () => false)
      .with([false, { share: true }, P.not("Date")], () => true)
      .with([f…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@harshmandan
Comment options

Answer selected by harshmandan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants