Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make isMatching(p) infer the pattern as a const type parameter #221

Merged
merged 3 commits into from
Feb 2, 2024

Conversation

gvergnaud
Copy link
Owner

@gvergnaud gvergnaud commented Feb 2, 2024

Motivation

Currently, you need to use as const on your pattern to make sure that type narrowing works as you intend:

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

declare const x: Food

const isPizza = isMatching({ type: 'pizza' })
const isPizzaConst = isMatching({ type: 'pizza' } as const)

if (isPizza(x)) x  // x: Food
if (isPizzaConst(x)) x  // x: Pizza

This isn't intentional. Th 2-arguments overload of isMatching doesn't have this problem.

This PR fixes that.

@gvergnaud gvergnaud merged commit a287f41 into main Feb 2, 2024
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.

None yet

1 participant