Conversation
`isMatching` intersected `UnknownProperties` with the whole `P.Pattern<T>` union to let object patterns target properties the input type does not declare. That intersection also reached the matcher member of the union, so a top level matcher such as `P.instanceOf(Text)` was rejected because it has no index signature. Distribute over the pattern union and only intersect `UnknownProperties` with the non-matcher members. Fixes gvergnaud#336
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
Thanks — valid point, fixed in The assertion was indeed vacuous:
I used a second class rather than
|
There was a problem hiding this comment.
All reported issues were addressed across 1 file (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
Good catch again — the annotation form was still vacuous, confirmed and fixed in Two separate problems were hiding each other:
Verified it is no longer vacuous: swapping the guard to |
Fixes #336
Problem
isMatching(P.instanceOf(Text), document.createTextNode('123'))fails to type check:The two-argument overload of
isMatchingconstrains the pattern with:The
& UnknownPropertiespart exists so object patterns can target properties theinput type does not declare (covered by the existing "should allow targetting
unknown properties" test). But
P.Pattern<T>is a union whose members includePatternMatcher<T>, and the intersection applies to every member — includingthe matcher one. Matchers have no index signature, so any top level matcher is
rejected whenever the input happens to be an object. This affects
P.instanceOf(...),P.any,P.when(...),P.not(...), and so on.Fix
Distribute over the pattern union and intersect
UnknownPropertiesonly with themembers that are not matchers:
Object patterns keep their unknown-property escape hatch, and top level matchers
are accepted again.
Note on the
@ts-expect-errormovePatternConstraintis now a distributive conditional type, so TypeScript reportsthe invalid-pattern error on the offending property rather than on the whole object
literal. The error is still raised — the
@ts-expect-errorin"should reject invalid pattern when two parameters are passed" just moved one line
in to stay attached to it. Removing the directive entirely makes that test fail, so
the rejection behaviour is unchanged.
Validation
npx jest— 48 suites, 454 tests, all passing (14 inis-matching.test.ts, up from 13).tsc --strict --noEmitclean.main.New test
should accept a top level matcher when the input is an objectcoversP.instanceOf,P.any,P.whenandP.notat the top level, with anEqualassertion that narrowing still produces the expected type.
Summary by cubic
Fixes #336 —
isMatchingnow accepts top-level matchers (P.instanceOf,P.any,P.when,P.not) when the input is an object. The old constraint intersectedUnknownPropertieswith the entire pattern union, rejecting matchers for lacking an index signature; it now applies only to non-matcher members, preserving unknown-property support for object patterns.The
@ts-expect-errorin the invalid-pattern test moves one line down because the error now surfaces on the property; rejection behavior is unchanged. The new test covers all four matchers with the input widened to a union, and includes anEqualassertion that the guard removes the other union member.Written for commit 01faa63. Summary will update on new commits.