npm ts-pattern 2.1.0
v2.1.0

latest releases: 5.2.0, 5.1.2, 5.1.1...
3 years ago

This version introduces a new API to opt into exhaustive pattern matching, to let typescript make sure that all possible cases are handled and that your code won't throw at runtime.

Here is an example of this new API:

type Input = { kind: 'none' } | { kind: 'some'; value: number };

// This compiles:
match({ kind: 'some', value: 3 })
.exhaustive()
.with({ kind: 'some' }, ({ value }) => value)
.with({ kind: 'none' }, () => 0)
.run();

// This doesn't
match({ kind: 'some', value: 3 })
.exhaustive()
.with({ kind: 'some' }, ({ value }) => value)
.run();

This is a major version because it uses some features of the type system that were introduced in TS v4.x, like variadic tuples, and recursive conditional types.

Don't miss a new ts-pattern release

NewReleases is sending notifications on new releases.