v5.7.1
Type inference bug fixes
This new release fixes the following bug in exhaustiveness checking when matching on optional properties:
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:
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
- fix(.exhaustive): optional discriminant by @gvergnaud in #319
Full Changelog: v5.7.0...v5.7.1