npm ts-pattern 5.7.0
v5.7.0

9 days ago

New feature

Exhaustive callback

By default, .exhaustive() will throw an error if the input value wasn't handled by any .with(...) clause. This should only happen if your types are incorrect.

It is possible to pass your own handler function as a parameter to decide what should happen if an unexpected value has been received. You can for example throw your own custom error:

match(...)
  .with(...)
  .exhaustive((unexpected: unknown) => {
    throw MyCustomError(unexpected);
  })

Or log an error and return a default value:

match<string, number>(...)
  .with(P.string, (str) => str.length)
  .exhaustive((notAString: unknown) => {
    console.log(`received an unexpected value: ${notAString}`);
    return 0;
  })

Improved narrowing for isMatching

isMatching didn't have full feature parity with match in terms of type narrowing, but now does.

What's Changed

  • build(deps-dev): bump bun from 1.0.4 to 1.1.30 in /benchmarks by @dependabot in #303
  • isMatching: improve narrowing by @gvergnaud in #311
  • feat(exhaustive): Add support for passing a fallback function by @gvergnaud in #253

Full Changelog: v5.6.2...v5.7.0

Don't miss a new ts-pattern release

NewReleases is sending notifications on new releases.