This release contains two changes:
Typecheck pattern when using isMatching with 2 parameter.
It used to be possible to pass a pattern than could never match to isMatching
. The new version checks that the provide pattern does match the value in second parameter:
type Pizza = { type: 'pizza'; topping: string };
type Sandwich = { type: 'sandwich'; condiments: string[] };
type Food = Pizza | Sandwich;
const fn = (food: Pizza | Sandwich) => {
if (isMatching({ type: 'oops' }, food)) {
// 👆 used to type-check, now doesn't!
}
}
Do not use P.infer
as an inference point
When using P.infer<Pattern>
to type a function argument, like in the following example:
const getWithDefault = <T extends P.Pattern>(
input: unknown,
pattern: T,
defaultValue: P.infer<T> // 👈
): P.infer<T> =>
isMatching(pattern, input) ? input : defaultValue
TypeScript could get confused and find type errors in the wrong spot:
const res = getWithDefault(null, { x: P.string }, 'oops')
// 👆 👆 type error should be here
// but it's here 😬
This new version fixes this problem.
What's Changed
- Improvements to
P.infer
andisMatching
by @gvergnaud in #302 - build(deps-dev): bump rollup from 2.79.1 to 2.79.2 by @dependabot in #289
Full Changelog: v5.5.0...v5.6.0