std match takes an Option of an enum (undefined handler, _ covers it) #49

Merged
sionzee merged 2 commits from evobug/match-option into main 2026-11-16 17:46:53 +01:00
Owner

What

std's match takes an Option of an enum, like Rust's match opt { Some(..) => .., None => .. }:

return match(u.job, { gather: (job) => ..., build: (job) => ..., undefined: () => undefined });
  • Types (packages/std/src/enum.ts): when the value's type includes undefined, the handlers may have an undefined: () => R handler. _ covers a missing value too, so its parameter includes undefined unless there is an undefined handler. The check is exhaustive: a value that may be missing needs undefined or _, and an undefined handler on a value that can't be missing is a compile error. It is still one signature, not overloads. OnlyKeys, Fallback/Rest, the expected-type R and the NoInfer handling all work as before. is and variant take E | undefined and give false / undefined for a missing value.
  • Runtime match: uses the undefined handler, else _, else unreachable.
  • Build-time rewrite (matcher.rs): switch (__ms?.tag) with case undefined:. ?. is used whenever there is an undefined arm or _, so default: gets a missing value like the runtime does. In TypeScript files the case is case undefined satisfies typeof __ms: and _'s binding is __ms! after an undefined arm. That way the emitted code type-checks the same way the call does: TypeScript doesn't narrow undefined out of default: after case undefined, and without satisfies the "can't be missing" error would disappear from emitted output (typecheck:emit caught both). is/variant now become x?.tag === ....
  • Dev checks: the match check accepts undefined when the match has an undefined handler.
  • Checker: nothing to change for match in TS. The checker has no rule on match handler keys, and check with idiomatic rules on reports nothing for the new form. check_enum_use is only used for Vue.

Not done: Vue <Match>

There is no #undefined slot. The runtime and template rewrite parts would be easy. The exhaustiveness part would not: vue.rs's Ty doesn't track whether a named enum value may be undefined, so it couldn't require or reject the slot. #undefined is still reported as "not a variant" (checked). The docs say to wrap <Match> in v-if / v-else.

Docs changed

  • docs/guide/enums.md: new section "Matching an Option" (workTarget example, _ covering undefined, exhaustiveness, is/variant). The "In short" list and the rewrite text (?.tag) are updated.
  • docs/guide/dev-checks.md: what the match check accepts, and the "In short" list.
  • docs/guide/transforms.md: the per-rewrite table rows for match and is/variant.
  • docs/vue/async.md, docs/vue/rules/vue-match.md: <Match> doesn't take a missing value.
  • README.md: Enums section (Option example; ?.tag in the rewrite description).
  • CHANGELOG.md: an Unreleased / Added entry.
  • Doc comments: match, is, variant (std) and the Match component (safe-ts-vue).

Verification

  • packages/std/test/enum.check.ts: type tests for each case, including the errors (@ts-expect-error): missing undefined/_, undefined on a value that can't be missing (also next to _), a missing variant next to undefined, a handler that takes a parameter, misspelled variants, the expected result type, what _ receives, and is/variant narrowing.
  • enum.test.ts, dev-checks.test.ts: runtime behaviour, both rewritten and as a runtime call.
  • tests/transform.rs: exact output (TS and JS), plus a test that runs a file with Node both as written and as rewritten and compares the results.
  • Passing: cargo nextest run --workspace (184), npm run build, npm run test:std (245), npm run typecheck (with typecheck:emit), node scripts/docs-typecheck.mjs (344 examples), npm run test:docs. cargo clippy: nothing new in the changed code.
  • On TwitchRTS's workTarget / villagerTarget shape: copied into a scratch project, safe-ts check with idiomatic rules on reports nothing, and tsc passes.

🤖 Generated with Claude Code

## What std's `match` takes an `Option` of an enum, like Rust's `match opt { Some(..) => .., None => .. }`: ```ts return match(u.job, { gather: (job) => ..., build: (job) => ..., undefined: () => undefined }); ``` - **Types** (`packages/std/src/enum.ts`): when the value's type includes `undefined`, the handlers may have an `undefined: () => R` handler. `_` covers a missing value too, so its parameter includes `undefined` unless there is an `undefined` handler. The check is exhaustive: a value that may be missing needs `undefined` or `_`, and an `undefined` handler on a value that can't be missing is a compile error. It is still one signature, not overloads. `OnlyKeys`, `Fallback`/`Rest`, the expected-type `R` and the `NoInfer` handling all work as before. `is` and `variant` take `E | undefined` and give `false` / `undefined` for a missing value. - **Runtime `match`**: uses the `undefined` handler, else `_`, else `unreachable`. - **Build-time rewrite** (`matcher.rs`): `switch (__ms?.tag)` with `case undefined:`. `?.` is used whenever there is an `undefined` arm or `_`, so `default:` gets a missing value like the runtime does. In TypeScript files the case is `case undefined satisfies typeof __ms:` and `_`'s binding is `__ms!` after an `undefined` arm. That way the emitted code type-checks the same way the call does: TypeScript doesn't narrow `undefined` out of `default:` after `case undefined`, and without `satisfies` the "can't be missing" error would disappear from emitted output (`typecheck:emit` caught both). `is`/`variant` now become `x?.tag === ...`. - **Dev checks**: the `match` check accepts `undefined` when the match has an `undefined` handler. - **Checker**: nothing to change for `match` in TS. The checker has no rule on `match` handler keys, and `check` with idiomatic rules on reports nothing for the new form. `check_enum_use` is only used for Vue. ## Not done: Vue `<Match>` There is no `#undefined` slot. The runtime and template rewrite parts would be easy. The exhaustiveness part would not: `vue.rs`'s `Ty` doesn't track whether a named enum value may be `undefined`, so it couldn't require or reject the slot. `#undefined` is still reported as "not a variant" (checked). The docs say to wrap `<Match>` in `v-if` / `v-else`. ## Docs changed - `docs/guide/enums.md`: new section "Matching an `Option`" (workTarget example, `_` covering `undefined`, exhaustiveness, `is`/`variant`). The "In short" list and the rewrite text (`?.tag`) are updated. - `docs/guide/dev-checks.md`: what the `match` check accepts, and the "In short" list. - `docs/guide/transforms.md`: the per-rewrite table rows for `match` and `is`/`variant`. - `docs/vue/async.md`, `docs/vue/rules/vue-match.md`: `<Match>` doesn't take a missing value. - `README.md`: Enums section (Option example; `?.tag` in the rewrite description). - `CHANGELOG.md`: an Unreleased / Added entry. - Doc comments: `match`, `is`, `variant` (std) and the `Match` component (safe-ts-vue). ## Verification - `packages/std/test/enum.check.ts`: type tests for each case, including the errors (`@ts-expect-error`): missing `undefined`/`_`, `undefined` on a value that can't be missing (also next to `_`), a missing variant next to `undefined`, a handler that takes a parameter, misspelled variants, the expected result type, what `_` receives, and `is`/`variant` narrowing. - `enum.test.ts`, `dev-checks.test.ts`: runtime behaviour, both rewritten and as a runtime call. - `tests/transform.rs`: exact output (TS and JS), plus a test that runs a file with Node both as written and as rewritten and compares the results. - Passing: `cargo nextest run --workspace` (184), `npm run build`, `npm run test:std` (245), `npm run typecheck` (with `typecheck:emit`), `node scripts/docs-typecheck.mjs` (344 examples), `npm run test:docs`. `cargo clippy`: nothing new in the changed code. - On TwitchRTS's `workTarget` / `villagerTarget` shape: copied into a scratch project, `safe-ts check` with idiomatic rules on reports nothing, and tsc passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
std match takes an Option of an enum: undefined handler, _ covers a missing value
All checks were successful
Benchmarks / bench (pull_request) Successful in 1m56s
928380ee99
`match(value, ...)` accepts `E | undefined`. An `undefined: () => R` handler says what a
missing value gives; `_` covers it otherwise. Exhaustive at compile time: a value that may be
missing needs one of the two, an `undefined` handler on one that can't is an error. Still one
signature. `is` / `variant` accept an Option (false / undefined for a missing value).

Build-time rewrite: `switch (__ms?.tag)` with `case undefined satisfies typeof __ms:` (TS),
`_` bound with `!` after an `undefined` arm, so the emitted code type-checks like the call.
`is` / `variant` become `x?.tag === ...`. Dev checks accept undefined when there is an
`undefined` handler. Vue `<Match>` is unchanged (no `#undefined` slot), documented.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge remote-tracking branch 'origin/main' into up-49
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m7s
e556087301
sionzee deleted branch evobug/match-option 2026-11-16 17:46:53 +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!49
No description provided.