enum-method-escape
A value of an enum with methods given where its type isn't the enum's but names one of the methods: an argument for a parameter declared with an object type or interface that has the method, or a binding declared with one. On by default, as an error.
Why: an enum's values have no methods of their own; the build turns a call into a call of the enum's table only where the declarations say the value is the enum. Through { readonly describe: () => string } the value is any object with a describe: TypeScript accepts the Job, and the code there calls thing.describe() on a plain object that has none. Guide: Methods on an enum.
✕ Rejectedenum-method-escape
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}` }),
});
type Describable = { readonly describe: () => string };
function shout(thing: Describable): string {
return `${thing.describe()}!`;
}
export const loud = shout(Job.Queued());ts
import type { Data as __EnumData } from "@evobug_com/safe-ts-std/enum"; 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}` }),
});
type Describable = { readonly describe: () => string };
function shout(thing: Describable): string {
return `${thing.describe()}!`;
}
export const loud = shout(({ tag: "Queued" } satisfies __EnumData<Job> as Job));✅ Accepted: the enum declared where it is used, or what the method gives passed on
✓ 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}` }),
});
function shout(job: Job): string {
return `${job.describe()}!`;
}
function shoutText(text: string): string {
return `${text}!`;
}
export const loud = shout(Job.Queued());
export const louder = shoutText(Job.Queued().describe());ts
import type { Data as __EnumData } from "@evobug_com/safe-ts-std/enum"; 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}` }),
});
function shout(job: Job): string {
return `${Job.describe(job)}!`;
}
function shoutText(text: string): string {
return `${text}!`;
}
export const loud = shout(({ tag: "Queued" } satisfies __EnumData<Job> as Job));
export const louder = shoutText(Job.describe(({ tag: "Queued" } satisfies __EnumData<Job> as Job)));What the rule can't see: a value passed to a generic function whose type parameter is constrained by such a type (<T extends Describable>(x: T)), a value stored in a field or returned where such a type is declared. Declare the enum there.