prefer-is-over-tag
x.tag === "Ready", x.tag !== "Failed" and switch (x.tag), in code and in Vue templates. An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.
Why: an enum can be read two ways, and both narrow the type: the helpers (is, variant, match) and the raw tag. A codebase that uses both reads like two codebases, and an agent copying the nearest example keeps both alive. The helpers say more: match fails to compile when a variant is added, variant hands over the fields, and is takes the variant's constructor, so a renamed variant is a compile error rather than a string that matches nothing. Guide: Enums.
✕ Rejectedprefer-is-over-tag
ts
import type { Enum } from "@evobug_com/safe-ts-std/enum";
type Upload = Enum<{ Queued: {}; Sending: { sent: number }; Failed: { reason: string } }>;
export function progress(upload: Upload): number {
if (upload.tag === "Queued") return 0;
return upload.tag === "Failed" ? -1 : upload.sent;
}✓ Accepted0 diagnostics
ts
import { type Enum, is, match } from "@evobug_com/safe-ts-std/enum";
import type { Option } from "@evobug_com/safe-ts-std/result";
type Upload = Enum<{ Queued: {}; Sending: { sent: number }; Failed: { reason: string } }>;
export function progress(upload: Upload): Option<number> {
return match(upload, { Queued: () => 0, Sending: ({ sent }) => sent, Failed: () => undefined });
}
export function waiting(upload: Upload): boolean {
return is(upload, "Queued");
}ts
import { unreachable as __matchUnreachable } from "@evobug_com/safe-ts-std/enum"; import { type Enum, is, match } from "@evobug_com/safe-ts-std/enum";
import type { Option } from "@evobug_com/safe-ts-std/result";
type Upload = Enum<{ Queued: {}; Sending: { sent: number }; Failed: { reason: string } }>;
export function progress(upload: Upload): Option<number> {
{ const __ms1 = upload; let __mv1: Option<number>; switch (__ms1.tag) { case "Queued": { __mv1 = 0; break; } case "Sending": { const { sent } = __ms1; __mv1 = sent; break; } case "Failed": { __mv1 = undefined; break; } default: __mv1 = __matchUnreachable(__ms1); } return __mv1; }
}
export function waiting(upload: Upload): boolean {
return (upload?.tag === "Queued");
}In a template, v-if="is(state, AsyncState.Ready)" narrows state inside the block as the .tag test does, and a v-if chain of is is checked for missing variants like a chain on .tag (vue-exhaustive-v-if):
✕ Rejectedprefer-is-over-tag
vue
<script setup lang="ts">
import type { Enum } from "@evobug_com/safe-ts-std/enum";
type Payment = Enum<{ Pending: {}; Paid: { receipt: string } }>;
defineProps<{ payment: Payment }>();
</script>
<template>
<p v-if="payment.tag === 'Paid'">Receipt {{ payment.receipt }}</p>
</template>✓ Accepted0 diagnostics
vue
<script setup lang="ts">
import { type Enum, is } from "@evobug_com/safe-ts-std/enum";
type Payment = Enum<{ Pending: {}; Paid: { receipt: string } }>;
defineProps<{ payment: Payment }>();
</script>
<template>
<p v-if="is(payment, 'Paid')">Receipt {{ payment.receipt }}</p>
</template>Only a string that reads as a variant name counts: an identifier in PascalCase, as constructors and union-to-enum write them. So release.tag === "v1.2.0" and node.tag === "input" aren't reported, and neither is an enum whose variants are lowercase ("ship"). The checker has no type information, so any .tag compared with such a name counts, also on a hand-written { tag: "A" } | { tag: "B" } union (which is and match take too). Every switch (x.tag) counts. Quick fix (in .ts files): Use is(x, "Variant"), !is(...) for !==, with the tag in the quotes it was written with. Where the enum has constructors in scope, the fix names the variant by its constructor instead, Use is(x, Status.Variant), which prefer-constructor-tag would otherwise ask for next: the same table that rule finds (the value's declared enum, or the only table in scope with that variant), and when the file imports the enum type-only, the fix imports it as a value. It imports is unless the file has it, or the prelude is on (the package's tsconfig.json lists @evobug_com/safe-ts-prelude in types; not in a file a Vite config loads, which runs untransformed); an import it adds uses the file's quotes and semicolons, those of its imports or, in a file without imports, of its code. No fix is offered where the value is a variable or parameter declared with null (x: Status | null): is takes Status | undefined, so is(x, ...) wouldn't compile. Turn the null into undefined where it comes in (then is(x, ...)), or write is(x ?? undefined, ...). A field or a call typed with null isn't seen, and gets the fix that tsc then refuses. In a template, the vue-tsc note in Async state applies: inside an event handler of a v-else-if branch under v-for, give the branch its own v-if.