Vendored Matcher is missing .returnType<T>() (regression vs beta.5)
Summary
5.0.0-beta.6 replaces the ts-pattern re-export with unthrown's own vendored matcher — great change, and it fixed #145 (a generic E now composes, confirmed). But the vendored Matcher has no .returnType<T>(), which the ts-pattern Match it replaced did have. That is a capability regression: beta.5 supported it, beta.6 does not.
Reproduction
import { Err, tag, TaggedError } from "unthrown";
class NotFoundError extends TaggedError("NotFound") {}
class ConflictError extends TaggedError("Conflict") {}
type Response =
| { status: 404; body: { code: "NOT_FOUND" } }
| { status: 409; body: { code: "CONFLICT" } };
declare const result: import("unthrown").Result<number, NotFoundError | ConflictError>;
result.recoverErrCases((matcher) =>
matcher
.returnType<Response>()
.with(tag("NotFound"), () => ({ status: 404, body: { code: "NOT_FOUND" } }))
.with(tag("Conflict"), () => ({ status: 409, body: { code: "CONFLICT" } })),
);
error TS2339: Property 'returnType' does not exist on type
'Matcher<NotFoundError | ConflictError, NotFoundError | ConflictError, never>'.
plus knock-on TS7006: Parameter '...' implicitly has an 'any' type on the arms, because the expected return type is gone.
Why it matters
.returnType<T>() is what makes an Err-fold checked against a declared output type rather than merely inferred. The pattern above is how a typed HTTP layer pins every arm to the route's response union: without it, each arm is inferred structurally and nothing verifies it against the contract — a wrong status code or a mistyped body discriminant stops being a compile error.
That is the only place we use returnType, but we use it at every such fold, so the upgrade is blocked on it.
Suggested fix
Add returnType to the vendored Matcher, matching ts-pattern's semantics — it constrains subsequent .with(...) handler return types and fixes the builder's output type:
returnType<O2>(): Matcher<E, Remaining, O2>;
with .with then checking each handler's return against O2 rather than widening O.
Other beta.6 notes (no action needed)
#145is confirmed fixed — a helper generic over its error type now type-checks with.match({ ok, errCases, defect }). Thank you.- The reduced
Psurface (_,any,instanceOf,when,union,string,number) is fine for Err matching on our side; we only useP._andP.unionplus the variadic.with(a, b, handler). @unthrown/patternis still not deprecated on npm (see #146).
We are holding our upgrade on beta.5 until this lands, since dropping returnType would mean hand-annotating every arm and losing the output-type check.
Environment
[email protected], TypeScript 7.0.1-rc,strict: true