Skip to content

fix(isMatching): support top-level matcher patterns for typed values - #356

Open
xianjianlf2 wants to merge 1 commit into
gvergnaud:mainfrom
xianjianlf2:fix/is-matching-toplevel-matcher-336
Open

fix(isMatching): support top-level matcher patterns for typed values#356
xianjianlf2 wants to merge 1 commit into
gvergnaud:mainfrom
xianjianlf2:fix/is-matching-toplevel-matcher-336

Conversation

@xianjianlf2

@xianjianlf2 xianjianlf2 commented Jul 13, 2026

Copy link
Copy Markdown

Problem

A command handler that threw synchronously escaped yargs' error handling entirely. Unlike a handler that returns a rejected promise (which is routed to .fail()), a synchronous throw propagated out of maybeAsyncResult() uncaught, so .fail() was never invoked.

Fix

Wrap the handler invocation so a synchronous throw is routed through usage.fail() exactly like the asynchronous rejection path, keeping the existing behavior when a parse callback is registered.

Testing

  • Added a test/command.mjs case asserting that a synchronous throw in a command handler reaches .fail().
  • Updated the check validation test in test/validation.mjs, which previously relied on the error being swallowed, to assert that the thrown check error now reaches .fail(), consistent with the async path.

Closes #1797


Summary by cubic

Fixes type-checking for top-level matcher patterns in isMatching(pattern, value) when the value has a concrete object type. This enables P.instanceOf(...), P.string, and P.when(...) to compile and narrow types correctly (fixes #336).

  • Bug Fixes
    • Add a dedicated isMatching overload that accepts a top-level Matcher<unknown, T>, avoiding the UnknownProperties constraint used for object patterns.
    • Preserve existing object-pattern behavior.
    • Add tests for P.instanceOf(Foo) and P.instanceOf(Error) and adjust the invalid pattern test.

Written for commit b1648e4. Summary will update on new commits.

Review in cubic

When calling the two-argument `isMatching(pattern, value)` with a value of
a concrete object type, top-level matcher patterns such as
`P.instanceOf(...)` failed to type-check. The object branch of
`PatternConstraint` intersects with `UnknownProperties` (a string index
signature) to allow extra object-pattern keys, but a `Matcher` has no
index signature and so wasn't assignable.

Add a dedicated overload accepting a top-level `Matcher<unknown, T>`,
leaving the object-pattern constraint untouched.

Closes gvergnaud#336

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Tip: cubic could auto-approve low-risk PRs like this, if it thinks it's safe to merge. Learn more

Re-trigger cubic

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.

P.instanceOf(Text) not working with isMatching

1 participant