std match takes an Option of an enum (undefined handler, _ covers it) #49
Loading…
Reference in a new issue
No description provided.
Delete branch "evobug/match-option"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
std's
matchtakes anOptionof an enum, like Rust'smatch opt { Some(..) => .., None => .. }:packages/std/src/enum.ts): when the value's type includesundefined, the handlers may have anundefined: () => Rhandler._covers a missing value too, so its parameter includesundefinedunless there is anundefinedhandler. The check is exhaustive: a value that may be missing needsundefinedor_, and anundefinedhandler on a value that can't be missing is a compile error. It is still one signature, not overloads.OnlyKeys,Fallback/Rest, the expected-typeRand theNoInferhandling all work as before.isandvarianttakeE | undefinedand givefalse/undefinedfor a missing value.match: uses theundefinedhandler, else_, elseunreachable.matcher.rs):switch (__ms?.tag)withcase undefined:.?.is used whenever there is anundefinedarm or_, sodefault:gets a missing value like the runtime does. In TypeScript files the case iscase undefined satisfies typeof __ms:and_'s binding is__ms!after anundefinedarm. That way the emitted code type-checks the same way the call does: TypeScript doesn't narrowundefinedout ofdefault:aftercase undefined, and withoutsatisfiesthe "can't be missing" error would disappear from emitted output (typecheck:emitcaught both).is/variantnow becomex?.tag === ....matchcheck acceptsundefinedwhen the match has anundefinedhandler.matchin TS. The checker has no rule onmatchhandler keys, andcheckwith idiomatic rules on reports nothing for the new form.check_enum_useis only used for Vue.Not done: Vue
<Match>There is no
#undefinedslot. The runtime and template rewrite parts would be easy. The exhaustiveness part would not:vue.rs'sTydoesn't track whether a named enum value may beundefined, so it couldn't require or reject the slot.#undefinedis still reported as "not a variant" (checked). The docs say to wrap<Match>inv-if/v-else.Docs changed
docs/guide/enums.md: new section "Matching anOption" (workTarget example,_coveringundefined, exhaustiveness,is/variant). The "In short" list and the rewrite text (?.tag) are updated.docs/guide/dev-checks.md: what thematchcheck accepts, and the "In short" list.docs/guide/transforms.md: the per-rewrite table rows formatchandis/variant.docs/vue/async.md,docs/vue/rules/vue-match.md:<Match>doesn't take a missing value.README.md: Enums section (Option example;?.tagin the rewrite description).CHANGELOG.md: an Unreleased / Added entry.match,is,variant(std) and theMatchcomponent (safe-ts-vue).Verification
packages/std/test/enum.check.ts: type tests for each case, including the errors (@ts-expect-error): missingundefined/_,undefinedon a value that can't be missing (also next to_), a missing variant next toundefined, a handler that takes a parameter, misspelled variants, the expected result type, what_receives, andis/variantnarrowing.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.cargo nextest run --workspace(184),npm run build,npm run test:std(245),npm run typecheck(withtypecheck:emit),node scripts/docs-typecheck.mjs(344 examples),npm run test:docs.cargo clippy: nothing new in the changed code.workTarget/villagerTargetshape: copied into a scratch project,safe-ts checkwith idiomatic rules on reports nothing, and tsc passes.🤖 Generated with Claude Code
undefinedhandler,_covers a missing value