Skip to main content

Deep Match

Sometimes it is useful in TypeScript to have have a complex condition on a (nested) object. This can lead to long if statements that are also hard to read.

type Person = {  name: string;  age: number;  address: {    street: string;    city: string;  };};
function process(person: Person) {  if (    person.age < 18 &&    person.name === 'Bart' &&    person.address.city === 'Springfield'  ) {    console.log('you shall pass');  }}

The Match object offers, in a similar fashion to Patch, a way to concisely define the conditions an (immutable) object should meet:

import { Match } from '@rimbu/core';
function process(person: Person) {  if (    Match.any(person)({      age: (v) => v < 18,      name: 'Bart',      address: {        city: 'Springfield',      },    })  ) {    console.log('you shall pass');  }}

The following CodeSandbox shows in more detail how Match can be used for more complex use cases:

Open file below in new window with full type-check