You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to be able to type check the pattern that I am passing to isMatching. This would help me avoid mistakes when defining the pattern I want to check.
See the following example:
const foo = { bar: 42 }
isMatching({ hello: P.string }, foo) // No ts-error
I would expect a type error here as hello: string is not part of the foo definition.
The following implementation would accomplish this but would lose the type guard:
I don't understand why in the original implementation value is unknown. We can parameterize isMatching with value type and keep type guarding too. Here is my implementation:
export function canMatch<T, const P extends Pattern.Pattern<T>>(
value: T,
pattern: P
): value is P.infer<P> {
return match(value)
.with(pattern, () => true)
.otherwise(() => false);
}
I would like to be able to type check the pattern that I am passing to isMatching. This would help me avoid mistakes when defining the pattern I want to check.
See the following example:
I would expect a type error here as
hello: string
is not part of thefoo
definition.The following implementation would accomplish this but would lose the type guard:
The text was updated successfully, but these errors were encountered: