Skip to content

fix(isMatching): Allow unknown properties in pattern#305

Merged
gvergnaud merged 2 commits intomainfrom
gvergnaud/fix-is-matching-union
Jan 19, 2025
Merged

fix(isMatching): Allow unknown properties in pattern#305
gvergnaud merged 2 commits intomainfrom
gvergnaud/fix-is-matching-union

Conversation

@gvergnaud
Copy link
Owner

@gvergnaud gvergnaud commented Jan 19, 2025

The previous release introduced a bug with isMatching which made it unable to narrow union types properly, and would make it reject patterns containing extra unknown properties. This PR fixes this

type Pizza = { type: 'pizza'; topping: string };
type Sandwich = { type: 'sandwich'; condiments: string[] };
type Food = Pizza | Sandwich;

const food = { type: 'pizza', topping: 'cheese' } as Food;

// These patterns used to be reject, but now work:

if (isMatching({ topping: 'cheese' }, food)) {
    // food: Food & { topping: 'cheese' }
}

if (isMatching({ unknownProp: P.instanceOf(Error) }, food)) {
// food: Food & { unknownProp: Error }
}

@gvergnaud gvergnaud marked this pull request as ready for review January 19, 2025 15:14
@gvergnaud gvergnaud merged commit c415e0c into main Jan 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant