Skip to content

no-overload ​

Overload signatures on the project's own functions and class methods (constructors too), reported once per function, at its first signature. On by default, as an error.

Why: TypeScript checks an overloaded function's body only against the implementation's signature. The overload signatures only have to be loosely related to it, and callers get what the overloads say:

ts
export function parse(text: string): User;           // what callers get
export function parse(text: unknown): unknown {      // what the body is checked against
  return text;                                       // compiles: a User nobody made
}

So an overload whose result differs from the implementation's is a cast (no-unchecked-cast) that doesn't look like one.

❌ Rejected: the second overload's number[] is never checked

✕ Rejectedno-overload
ts
export function parse(text: string): string;
export function parse(text: string, list: true): number[];
export function parse(text: string, list?: true): string | number[] {
  return list === true ? text.split(",").map(Number) : text.trim();
}

✅ Accepted: one function per signature, each checked against its own result

✓ Accepted0 diagnostics
ts
export function parseText(text: string): string {
  return text.trim();
}

export function parseList(text: string): number[] {
  return text.split(",").map(Number);
}

When callers must tell the cases apart from one call, return one Enum instead: each variant says what it holds, and match makes the caller handle both.

✅ Also accepted: one signature, the result an Enum

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

export type Parsed = Enum<{ Text: { readonly text: string }; List: { readonly items: readonly number[] } }>;
export const Parsed = constructors<Parsed>();

export function parse(text: string): Parsed {
  return text.includes(",") ? Parsed.List({ items: text.split(",").map(Number) }) : Parsed.Text({ text: text.trim() });
}
ts
import { constructors, type Enum } from "@evobug_com/safe-ts-std/enum";

export type Parsed = Enum<{ Text: { readonly text: string }; List: { readonly items: readonly number[] } }>;
export const Parsed = constructors<Parsed>();

export function parse(text: string): Parsed {
  return text.includes(",") ? ({ tag: "List", items: text.split(",").map(Number) } satisfies Parsed as Parsed) : ({ tag: "Text", text: text.trim() } satisfies Parsed as Parsed);
}

Overloads that only narrow the implementation ​

Allowed: every overload has the same type parameters and the same result type as the implementation, and each parameter's type is the implementation's or members of its union (and is optional only where the implementation's is). An overload may leave out parameters the implementation declares optional, and no others. TypeScript checks the body for everything such an overload accepts, and it returns what the overload says; the overloads only tell callers which combinations are valid, and give JSX and callbacks a precise type to infer from.

✓ Accepted0 diagnostics
ts
export function label(value: string): string;
export function label(value: number): string;
export function label(value: string | number): string {
  return typeof value === "number" ? value.toFixed(2) : value;
}

A result TypeScript can't compute ​

An overload over a loose implementation is how a function says "the result has this precise type" when the body can't produce it (a mapped type over the arguments, built with Object.fromEntries). That is a claim, not a check: say it with trust<T>(value) (std result), which is searchable and counted in safe-ts check's escape-hatch line, and keep the body short enough to see that it is true.

✕ Rejectedno-overload
ts
type CodesOf<T extends readonly string[]> = {
  readonly [I in keyof T as I extends `${number}` ? T[I] & string : never]: I extends `${infer N extends number}` ? N : never;
};

export function codesOf<const T extends readonly string[]>(names: T): CodesOf<T>;
export function codesOf(names: readonly string[]): Readonly<Record<string, number>> {
  return Object.freeze(Object.fromEntries(names.map((name, i) => [name, i])));
}
ts
type CodesOf<T extends readonly string[]> = {
  readonly [I in keyof T as I extends `${number}` ? T[I] & string : never]: I extends `${infer N extends number}` ? N : never;
};

export function codesOf<const T extends readonly string[]>(names: T): CodesOf<T>;
export function codesOf(names: readonly string[]): Readonly<Record<string, number>> {
  { const __la1 = names; 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 name = __le1; let i = __li1; __lr1[__li1] = ([name, i]); }} return Object.freeze(Object.fromEntries(__lr1)); }
}
✓ Accepted0 diagnostics
ts
import { trust } from "@evobug_com/safe-ts-std/result";

type CodesOf<T extends readonly string[]> = {
  readonly [I in keyof T as I extends `${number}` ? T[I] & string : never]: I extends `${infer N extends number}` ? N : never;
};

/** `{ Hello: 0, Bye: 1 }`: each name's code is its index. */
export function codesOf<const T extends readonly string[]>(names: T): CodesOf<T> {
  // `Object.fromEntries` can't say which keys it makes: each name, its index.
  return trust<CodesOf<T>>(Object.freeze(Object.fromEntries(names.map((name, i) => [name, i]))));
}

Where it isn't reported ​

  • .d.ts files, declare function, declare class, declare module, declare global and declare namespace: they describe code from elsewhere, which TypeScript can't check either.
  • Call signatures in an interface or a type literal (interface Parser { (text: string): User; (text: string, strict: true): Option<User> }): a function assigned to that type is checked against each of them.
  • abstract methods, and files marked // @safe-ts std (std's own overloads live there).

What the rule can't see ​

  • Whether a type parameter in an allowed overload is used the way the implementation's is: the type parameter lists have to be written the same, and are compared as text.
  • A union written through an alias (type Input = string | number): the members are compared as written, so label(value: string) over label(value: Input) is reported. Write the union out in the implementation's signature.

Related: no-type-predicate, the other signature TypeScript takes on trust.

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