github gvergnaud/ts-pattern v2.1.0

latest releases: v5.5.0, v5.4.0, v5.3.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.