Download Latest Version v5.9.2 source code.zip (323.1 kB) Google Add to Preferred Sources
Home / v5.7.1
Name Modified Size InfoDownloads / Week
Parent folder
README.md 2025-05-18 1.5 kB
v5.7.1 source code.tar.gz 2025-05-18 254.4 kB
v5.7.1 source code.zip 2025-05-18 290.1 kB
Totals: 3 Items   545.9 kB 0

Type inference bug fixes

This new release fixes the following bug in exhaustiveness checking when matching on optional properties:

:::ts
type Input = { type?: 'one' } | { type: 'two' };

const f1 = (input: Input) =>
  match(input)
    .with({ type: 'one' }, () => {})
    .with({ type: 'two' }, () => {})
    .exhaustive(); // shouldn't type-check, but does πŸ‘Ž


const f2 = (input: Input) =>
  match(input)
    .with({ type: 'one' }, () => {})
    .with({ type: 'two' }, () => {})
    .with({ type: undefined }, () => {}) // <- the type key needs to be present.
    .exhaustive();  // shouldn't type-check, but does πŸ‘Ž

These two cases don't type check anymore. They fail with a NonExhaustiveError<{ type?: undefined; }>. To fix it, you should do:

:::ts
type Input = { type?: 'one' } | { type: 'two' };

const f = (input: Input) =>
  match(input)
    .with({ type: 'one' }, () => {})
    .with({ type: 'two' }, () => {})
    .with({ type: P.optional(undefined) }, () => {}) // <- the type property may not be there
    .exhaustive(); // βœ…

This is a purely type-level change, the runtime behavior is still the same.

What's Changed

Full Changelog: https://github.com/gvergnaud/ts-pattern/compare/v5.7.0...v5.7.1

Source: README.md, updated 2025-05-18