Skip to content

key-presence-union ​

"key" in x, where x is declared as a union of the project's object types and only some of them have key: the variants are told apart by which keys they have. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.

Why: in narrows a union, so the code compiles, but nothing names the variants: a new member with the same key is taken for the old one, and adding an optional key to another member changes which branch runs. An Enum carries a tag, and match lists every variant a change leaves out. Guide: Enums.

✕ Rejectedkey-presence-union
ts
type IssueChoice = { readonly issueId: string; readonly title: string };
type BranchChoice = { readonly branch: string; readonly title: string };
type Choice = IssueChoice | BranchChoice;

export function link(choice: Choice): string {
  return "issueId" in choice ? `/issues/${choice.issueId}` : `/tree/${choice.branch}`;
}
✓ Accepted0 diagnostics
ts
import { type Enum, match } from "@evobug_com/safe-ts-std/enum";

type Choice = Enum<{ Issue: { issueId: string; title: string }; Branch: { branch: string; title: string } }>;

export function link(choice: Choice): string {
  return match(choice, { Issue: ({ issueId }) => `/issues/${issueId}`, Branch: ({ branch }) => `/tree/${branch}` });
}
ts
import { unreachable as __matchUnreachable } from "@evobug_com/safe-ts-std/enum"; import { type Enum, match } from "@evobug_com/safe-ts-std/enum";

type Choice = Enum<{ Issue: { issueId: string; title: string }; Branch: { branch: string; title: string } }>;

export function link(choice: Choice): string {
  { const __ms1 = choice; let __mv1: string; switch (__ms1.tag) { case "Issue": { const { issueId } = __ms1; __mv1 = `/issues/${issueId}`; break; } case "Branch": { const { branch } = __ms1; __mv1 = `/tree/${branch}`; break; } default: __mv1 = __matchUnreachable(__ms1); } return __mv1; }
}

Only a value whose declared type is such a union (a parameter or variable annotated with it, or with the members written there) is checked; a key every member has isn't reported. To convert, union-to-enum turns a union discriminated by a field into an Enum.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra