Skip to content

prefer-constructor-tag ​

is(x, "Published") and variant(x, "Published") where the enum has constructors in scope (const Status = constructors<Status>(), declared in the file or imported). An idiomatic rule: reported only with "safe-ts": { "idiomatic": true } in package.json.

Why: one spelling for a variant. is(x, "Published") and is(x, Status.Published) are the same test, and the build compiles both to x?.tag === "Published", so the choice costs nothing at runtime. The constructor is the one an editor can follow: a rename changes every use, and go-to-definition lands on the variant. A string is a second spelling that a rename skips. An agent copying the nearest example keeps both alive. An enum without constructors keeps the string form, which is how it is written everywhere else. Guide: Enums.

ts
import { constructors, type Enum, is } from "@evobug_com/safe-ts-std/enum";

type Status = Enum<{ Draft: {}; Published: { at: string } }>;
const Status = constructors<Status>();

export function isLive(status: Status): boolean {
  return is(status, "Published");
}
ts
import { constructors, type Enum, is } from "@evobug_com/safe-ts-std/enum";

type Status = Enum<{ Draft: {}; Published: { at: string } }>;
const Status = constructors<Status>();

export function isLive(status: Status): boolean {
  return (status?.tag === "Published");
}
✓ Accepted0 diagnostics
ts
import { constructors, type Enum, is, variant } from "@evobug_com/safe-ts-std/enum";

type Status = Enum<{ Draft: {}; Published: { at: string } }>;
const Status = constructors<Status>();

export function isLive(status: Status): boolean {
  return is(status, Status.Published);
}

export function publishedAt(status: Status): string {
  return variant(status, Status.Published)?.at ?? "not yet";
}
ts
import { constructors, type Enum, is, variant } from "@evobug_com/safe-ts-std/enum";

type Status = Enum<{ Draft: {}; Published: { at: string } }>;
const Status = constructors<Status>();

export function isLive(status: Status): boolean {
  return (status?.tag === "Published");
}

export function publishedAt(status: Status): string {
  return (status?.tag === "Published" ? status : undefined)?.at ?? "not yet";
}

✅ Also accepted: an enum without constructors is read by its string tags

✓ Accepted0 diagnostics
ts
import { type Enum, is } from "@evobug_com/safe-ts-std/enum";

type Light = Enum<{ On: {}; Off: {} }>;

export function lit(light: Light): boolean {
  return is(light, "On");
}
ts
import { type Enum, is } from "@evobug_com/safe-ts-std/enum";

type Light = Enum<{ On: {}; Off: {} }>;

export function lit(light: Light): boolean {
  return (light?.tag === "On");
}

Which enum a call is about: the value's declared type, when it is a plain name (status: Status, Option<Status>). Otherwise the only constructor table in scope that has all the call's variants, and only if no other enum in scope (declared in the file, or imported) has them too: the checker has no type information, and a value of that other enum may be the one tested. A tag that isn't an identifier ("in-progress") has no Status.X form and isn't reported.

Quick fix: Use Status.Published, for every tag of the call. When the file imports the enum type-only (import type { Status }) from the module that declares its constructors, the fix imports Status as a value instead: the table and the type share the name, so the type comes with it (import type { Light, Status } becomes import { type Light, Status }). prefer-is-over-tag's quick fix finds the table the same way and writes is(x, Status.Published) directly, so the two rules don't take two fixes; where it writes the string form, this rule has nothing to offer either.

Templates (v-if="is(x, 'Published')") aren't checked by this rule.

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