std: match() infers the union of its handlers' results #25

Merged
sionzee merged 1 commit from fix/std-match-inference into main 2026-11-04 21:22:10 +01:00
Owner

What

match() from @evobug/safe-ts-std/enum returned unknown whenever nothing around the call gave it a result type. Examples: const x = match(...), computed(() => match(...)), a React useMemo(() => match(...)).

Why: the signature was match<E, R>(value: E, handlers: Handlers<E, R>): R. Handlers<E, R> is a mapped type over E["tag"]. TypeScript can't list its properties while E is still being inferred, so it never infers R from the handlers. This is true for plain arrows, destructuring arrows and mixes of both. With tsc 7.0.2, even { A: () => 1, B: () => 2 } gives unknown. It looked like a problem with mixed arrows only because every existing test and doc example has a declared return type, and that is what supplied R.

Fix (packages/std/src/enum.ts):

  • The handler object is its own type parameter H, constrained by Handlers<E, unknown> (or HandlersWithFallback<E, unknown>). Handlers are still contextually typed, so destructuring still sees the variant's fields, and a missing variant is still a compile error.
  • The result is MatchResult<H>: the union of what the handlers return, with _ included.
  • Because H is inferred, the object literal no longer gets an excess-property check. OnlyKeys maps unknown keys to never, so Circel: ... next to _, or an extra variant, still fails.
  • The removed R also means _ no longer has to "fit" the named handlers' type. It simply joins the union. If a declared return type doesn't fit, the error is at the call.

The build-time rewrite (matcher.rs, match → switch) doesn't read these types. npm run typecheck runs enum.check.ts through safe-ts emit and tsc, and both the rewritten and the original forms typecheck.

Tests

packages/std/test/enum.check.ts has new type-level tests using an exact Equal<A, B> check:

  • plain arrows, destructuring arrows, mixed, with _, _ only
  • a match inside an arrow whose return type is inferred
  • an extra key next to a complete set of handlers (must fail)
  • a handler result that doesn't fit the declared return type (must fail)

The six positive cases failed before the fix (unknown).

Commands run:

  • tsc -p on every package and example tsconfig (std, std emit, safe-ts-vue, demo, demo emit): all pass
  • npm run typecheck (with emit): passes
  • cargo nextest run: 104 passed
  • npm run test:std: 130 passed
  • npm run test:vite: passes
  • npm run test:docs: passes

Docs

There are no workaround annotations for this on main. The doc examples all have declared return types, and the Vue computed(() => match(...)) examples just had an unknown result nobody read. #19 (React section, not merged yet) has annotations added for this. They can be dropped once both are in.

Uncertain

  • Error messages for a missing variant now come from the last overload ("Property '_' is missing"), as before. The old signature had the same order.
  • _'s parameter is still the whole enum, so _: ({ tag }) => tag adds every tag literal to the union, including handled ones.

🤖 Generated with Claude Code

## What `match()` from `@evobug/safe-ts-std/enum` returned `unknown` whenever nothing around the call gave it a result type. Examples: `const x = match(...)`, `computed(() => match(...))`, a React `useMemo(() => match(...))`. **Why:** the signature was `match<E, R>(value: E, handlers: Handlers<E, R>): R`. `Handlers<E, R>` is a mapped type over `E["tag"]`. TypeScript can't list its properties while `E` is still being inferred, so it never infers `R` from the handlers. This is true for plain arrows, destructuring arrows and mixes of both. With tsc 7.0.2, even `{ A: () => 1, B: () => 2 }` gives `unknown`. It looked like a problem with mixed arrows only because every existing test and doc example has a declared return type, and that is what supplied `R`. **Fix** (`packages/std/src/enum.ts`): - The handler object is its own type parameter `H`, constrained by `Handlers<E, unknown>` (or `HandlersWithFallback<E, unknown>`). Handlers are still contextually typed, so destructuring still sees the variant's fields, and a missing variant is still a compile error. - The result is `MatchResult<H>`: the union of what the handlers return, with `_` included. - Because `H` is inferred, the object literal no longer gets an excess-property check. `OnlyKeys` maps unknown keys to `never`, so `Circel: ...` next to `_`, or an extra variant, still fails. - The removed `R` also means `_` no longer has to "fit" the named handlers' type. It simply joins the union. If a declared return type doesn't fit, the error is at the call. The build-time rewrite (`matcher.rs`, `match` → `switch`) doesn't read these types. `npm run typecheck` runs `enum.check.ts` through `safe-ts emit` and tsc, and both the rewritten and the original forms typecheck. ## Tests `packages/std/test/enum.check.ts` has new type-level tests using an exact `Equal<A, B>` check: - plain arrows, destructuring arrows, mixed, with `_`, `_` only - a match inside an arrow whose return type is inferred - an extra key next to a complete set of handlers (must fail) - a handler result that doesn't fit the declared return type (must fail) The six positive cases failed before the fix (`unknown`). Commands run: - `tsc -p` on every package and example tsconfig (std, std emit, safe-ts-vue, demo, demo emit): all pass - `npm run typecheck` (with emit): passes - `cargo nextest run`: 104 passed - `npm run test:std`: 130 passed - `npm run test:vite`: passes - `npm run test:docs`: passes ## Docs There are no workaround annotations for this on `main`. The doc examples all have declared return types, and the Vue `computed(() => match(...))` examples just had an `unknown` result nobody read. #19 (React section, not merged yet) has annotations added for this. They can be dropped once both are in. ## Uncertain - Error messages for a missing variant now come from the last overload ("Property '_' is missing"), as before. The old signature had the same order. - `_`'s parameter is still the whole enum, so `_: ({ tag }) => tag` adds every tag literal to the union, including handled ones. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
std: match() infers the union of its handlers' results
All checks were successful
Benchmarks / bench (pull_request) Successful in 6m54s
51a896e6ff
`match<E, R>(value, handlers: Handlers<E, R>)` never inferred R from the
handlers: `Handlers` maps over `E["tag"]`, which TypeScript can't expand
while E is still being inferred, so it saw no properties to infer from. R
was `unknown` unless the call's context supplied it (a declared return
type), which is why every existing test and doc example passed, and why
`const x = match(...)` or `computed(() => match(...))` got `unknown`.

The handler object is now its own type parameter, constrained by
`Handlers<E, unknown>` (or the `_` variant), so handlers are still
contextually typed and a missing variant still fails. The result is the
union of what the handlers return, `_` included. Inferring the object
loses the excess-property check, so unknown keys are mapped to `never`
explicitly. The build-time rewrite doesn't read these types and is
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch fix/std-match-inference 2026-11-04 21:22:10 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!25
No description provided.