override-parameter
A class member that overrides a base class's method and asks for more than the method it replaces: a parameter written with a narrower type, a required one where the base's is optional (or where the base's rest parameter may be given nothing), or a mutable list where the base takes a readonly one. The override may be a method or an arrow function property, instance or static; the base's method may be abstract. Reported at the parameter. On by default, as an error.
Why: TypeScript compares an override with the member it overrides as an assignment, and where that member is declared as a method, it compares the parameters in both directions, whatever strictFunctionTypes says. So override admit(dog: Dog) over admit(animal: Animal) compiles, and a call through the base class (shelter.admit({ kind: "cat" }) with a Kennel in shelter) hands it what the base accepts: dog.bark() is a TypeError. The interface half of this hole is no-method-signature; this is the class half.
✕ Rejectedoverride-parameter
ts
type Animal = { readonly kind: string };
type Dog = { readonly kind: string; readonly bark: () => string };
export class Shelter {
admit(animal: Animal): string {
return animal.kind;
}
}
export class Kennel extends Shelter {
override admit(dog: Dog): string {
return dog.bark();
}
}✅ Accepted: the override takes what the base takes and checks inside
✓ Accepted0 diagnostics
ts
import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
export type Animal = Enum<{ Dog: { bark: string }; Cat: { purr: string } }>;
export const Animal = constructors<Animal>();
export class Shelter {
admit(animal: Animal): string {
return animal.tag;
}
}
export class Kennel extends Shelter {
override admit(animal: Animal): string {
return match(animal, { Dog: ({ bark }) => bark, Cat: () => "only dogs here" });
}
}ts
import { unreachable as __matchUnreachable } from "@evobug_com/safe-ts-std/enum"; import { constructors, type Enum, match } from "@evobug_com/safe-ts-std/enum";
export type Animal = Enum<{ Dog: { bark: string }; Cat: { purr: string } }>;
export const Animal = constructors<Animal>();
export class Shelter {
admit(animal: Animal): string {
return animal.tag;
}
}
export class Kennel extends Shelter {
override admit(animal: Animal): string {
{ const __ms1 = animal; let __mv1: string; switch (__ms1.tag) { case "Dog": { const { bark } = __ms1; __mv1 = bark; break; } case "Cat": { __mv1 = "only dogs here"; break; } default: __mv1 = __matchUnreachable(__ms1); } return __mv1; }
}
}✅ Accepted: the member is a property with a function type, which TypeScript checks strictly in every override (an override that asks for a Dog no longer compiles)
✓ Accepted0 diagnostics
ts
type Animal = { readonly kind: string };
export class Shelter {
readonly admit = (animal: Animal): string => animal.kind;
}
export class Kennel extends Shelter {
override readonly admit = (animal: Animal): string => `kennel: ${animal.kind}`;
}What TypeScript checks
Checked with tsc (strict, noUncheckedIndexedAccess, exactOptionalPropertyTypes, strictFunctionTypes). "Accepted" is a hole; this rule reports those cells.
| Override | Narrower parameter | Wider parameter | Added required parameter | Required where optional | T[] where readonly T[] | Narrower return | Wider return |
|---|---|---|---|---|---|---|---|
| a method over a base class's method | accepted | accepted (fine) | refused | accepted | accepted | accepted (fine) | refused |
| a method over an abstract method | accepted | accepted (fine) | refused | accepted | accepted | accepted (fine) | refused |
| a static method over a static method | accepted | accepted (fine) | refused | accepted | accepted | accepted (fine) | refused |
| an arrow function property over a method | accepted | accepted (fine) | refused | accepted | accepted | accepted (fine) | refused |
| a method over a function-typed property | refused (and TS2425) | accepted (fine) | refused | refused | refused | accepted (fine) | refused |
| an arrow function property over a function-typed property | refused | accepted (fine) | refused | refused | refused | accepted (fine) | refused |
| a class's method implementing an interface member written as a property | refused | accepted (fine) | refused | refused | refused | accepted (fine) | refused |
| a class's method implementing an interface member written as a method | accepted: no-method-signature | accepted (fine) | refused | accepted | accepted | accepted (fine) | refused |
| an object literal's method assigned to an interface written as a property | refused | accepted (fine) | refused | refused | refused | accepted (fine) | refused |
| an object literal's method assigned to an interface written as a method | accepted: no-method-signature | accepted (fine) | refused | accepted | accepted | accepted (fine) | refused |
The arity shapes, for a method over a base class's method (the same for the other rows TypeScript doesn't check):
| Base | Override | TypeScript | Here |
|---|---|---|---|
rest(...parts: string[]) | rest(first: string, ...parts: string[]) | accepted: base.rest() gives it nothing | reported |
rest(...parts: string[]) | rest(a: string, b?: string) | accepted | reported |
rest(...parts: string[]) | rest(a?: string, b?: string) | accepted (fine) | silent |
rest(...parts: string[]) | rest(...parts: [string, ...string[]]) | accepted | reported |
fixed(a: string, b?: string) | fixed(...parts: string[]) | accepted (fine) | silent |
fixed(a: string, b?: string) | fixed(a: string, ...more: string[]) | accepted (fine) | silent |
two(a: string, b: number) | two(...parts: string[]) | refused | reported too (b isn't a string) |
dflt(a = 0) | dflt(a: number) | accepted | reported |
tuple(...args: [string, number?]) | tuple(...args: [string, number]) | accepted | reported |
tuple(...args: [string, number?]) | tuple(text: string, count: number) | accepted | reported |
tuple(...args: [string, number?]) | tuple(text: string, count?: number) | accepted (fine) | silent |
What is compared
Parameters are compared by position. A rest parameter covers every position from where it starts (a tuple type's elements one by one, [string, number?], then its own rest), and the caller may give it nothing, so a required parameter there asks for more.
With no type checker, "narrower" isn't decidable, so each pair of types, written and then opened through type aliases and interfaces on both sides (the opener every rule shares), is answered yes (the override takes everything the base's parameter does), no, or unknown, and only a no is reported. Yes:
- the same type, also through an alias (
type Pet = Animal); unknown;- a union that holds every member of the base's type (
Animal | undefined,Animal | number); - an object type whose properties are a subset of the base's, with the same types or wider ones (it asks for less), also what an interface extends (
Animaloverinterface Dog extends Animal) and a part of an intersection (AnimaloverAnimal & { bark(): string }); - a read-only view of the base's type (
readonly string[]overstring[],Readonly<Animal>overAnimal); - a class the base's parameter class extends: the program's classes through their
extends, the platform's through the browser pack (EventoverUIEventoverMouseEvent,NodeoverElementoverHTMLElement,EventTarget; every class of lib.dom, checked in Chromium, Firefox and WebKit), the language's through its map (ErroroverTypeError), Node's and a pack's modules' through their API maps (ReadableoverDuplex); - one more parameter, if it is optional (
extra = 0), or fewer parameters.
Unknown, so silent: a class none of those describe (a package's, without a pack) compared with a class of another name, since a different name proves nothing there; a function type; a type the opener stops at.
The base's type parameters are bound to what the subclass writes (class Dogs extends Box<Dog>: put(item: T) takes a Dog), and a method's own type parameters are matched by position.
What the rule can't see
- A base class of a package or of the platform (
extends Container,extends WebSocket): its declarations aren't read. On an adopter, every override was of such a class. - A type it can't compare: a parameter either side writes no type for, a type the opener stops at (a conditional or mapped type,
typeof,keyof, an indexed access), a function type (compared only for being written the same), a package's class no pack describes compared with another name, and a base class's type parameter bound across files inside a larger type (put(items: readonly T[])of a base in another file). - A class whose base the engines disagree on:
CSSStyleRule,CSSPageRuleandCSSStyleDeclarationextend a class in lib.dom that some engine doesn't have yet, so their parent isn't recorded, and taking that parent where the base takes one of them is reported. - A class used where an unrelated class is expected, and an object literal assigned to a class type: TypeScript compares their methods both ways too.
- A member declared in an interface and implemented by a class: that is
no-method-signature's, at the interface.