enum-as-text
An enum value turned into text: ${job}, String(job), job + "...", jobs.join(", "), job.toString(). On by default, as an error.
Why: an enum's values are plain objects, so the text is "[object Object]", which no reader can use. Say what a value reads as with a method of the enum (describe, declared in Enum<V, M>) or a match where the text is shown. Guide: Methods on an enum.
✕ Rejectedenum-as-text
ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
export type Job = Enum<{ Queued: {}; Failed: { reason: string } }, { readonly describe: () => string }>;
export const Job = constructors<Job>().impl({
describe: (job) => match(job, { Queued: () => "queued", Failed: ({ reason }) => `failed: ${reason}` }),
});
export function status(job: Job, jobs: readonly Job[]): string {
return `now: ${job}, before: ${jobs.join(", ")}`;
}✅ Accepted: the text the enum gives
✓ Accepted0 diagnostics
ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
export type Job = Enum<{ Queued: {}; Failed: { reason: string } }, { readonly describe: () => string }>;
export const Job = constructors<Job>().impl({
describe: (job) => match(job, { Queued: () => "queued", Failed: ({ reason }) => `failed: ${reason}` }),
});
export function status(job: Job, jobs: readonly Job[]): string {
return `now: ${job.describe()}, before: ${jobs.map((j) => j.describe()).join(", ")}`;
}ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
export type Job = Enum<{ Queued: {}; Failed: { reason: string } }, { readonly describe: () => string }>;
export const Job = constructors<Job>().impl({
describe: (job): string => match(job, { Queued: () => "queued", Failed: ({ reason }) => `failed: ${reason}` }),
});
export function status(job: Job, jobs: readonly Job[]): string {
return `now: ${Job.describe(job)}, before: ${(() => { const __la1 = jobs; const __lr1: any[] = new Array(__la1.length); for (let __li1 = 0, __ln1 = __la1.length; __li1 < __ln1; __li1++) { if (!(__li1 in __la1)) continue; const __le1 = __la1[__li1]!; { let j = __le1; __lr1[__li1] = (Job.describe(j)); }} return __lr1.join(", "); })()}`;
}It applies to every enum, with methods or without. A value whose type the checker can't follow isn't checked: it gives "[object Object]" too, but it never fails, so it isn't worth a rule that would guess.