Skip to content

constructor-calls-override ​

A constructor, or an instance field's initializer, that calls a method a subclass overrides: directly (this.describe()) or through the class's own methods (this.setup(), which calls this.describe()). On by default, as an error. Reported by safe-ts check only, which sees the whole program; the editor and the lint plugin check one file at a time and don't see the subclasses in other files.

Why: new Named() runs Base's constructor first and sets Named's own fields only after it returns. A call on this dispatches to the subclass, so Named's override runs on an object whose fields are still undefined, whatever their types say. TypeScript accepts it without a cast: the field is declared, typed and initialized, just not yet.

✅ Accepted: the value is passed in, so construction runs no code a subclass replaced

✓ Accepted0 diagnostics
ts
export class Base {
  readonly label: string;
  constructor(label: string) {
    this.label = label;
  }
}

export class Named extends Base {
  private readonly name: string;
  constructor(name: string) {
    super(name.toUpperCase());
    this.name = name;
  }
  greeting(): string {
    return `${this.label}: ${this.name}`;
  }
}

❌ Rejected: Base's constructor calls describe, which Named overrides (the subclass is what makes it a finding, so the example isn't checked here on its own)

ts
export class Base {
  readonly label: string;
  constructor() {
    this.label = this.describe(); // constructor-calls-override
  }
  describe(): string {
    return "base";
  }
}

export class Named extends Base {
  private readonly name = "named";
  override describe(): string {
    return this.name.toUpperCase(); // TypeError: `name` isn't set yet when `Base`'s constructor runs
  }
}
text
`this.describe()` in the constructor of `Base` runs `describe`, which the subclass `Named`
overrides (src/named.ts:3:12): `Named`'s fields are set only after this constructor returns,
so the override sees them `undefined`

Other ways out, when the value has to come from the subclass:

  • make the method private (or #describe): no subclass can override it, and the rule follows private methods without reporting them;
  • compute the value where the object is made: a static factory (static create()) that calls the method after new, or a getter that computes it when read;
  • class ... extends stays allowed: error classes and framework base classes need it.

What counts ​

  • The calls: on this (this.m(), this.#m()) in the constructor's body and in the initializers of instance fields (readonly label = this.describe()), followed through the class's own and inherited methods. Reading or writing this.x counts too, where a subclass declares x as a getter or setter. A static method (Base.describe()) isn't called on the instance and doesn't count.
  • The subclasses: every class in the program that extends the class, directly or through others, across files (extends Base, extends models.Base from import * as models), with classes declared at the top level of a module (class, export class, export default class, const X = class ...).
  • Abstract methods: a call that reaches an abstract method is reported even with no subclass in sight: every concrete subclass overrides it.

When the subclasses can't all be known ​

The program safe-ts check reads is taken as all the code there is, so an exported class counts as overridden only where a subclass in the program overrides it. A subclass elsewhere is one of two kinds: in a file of this project the check doesn't reach, which check lists as not checked (add it as an entry); or in another program that imports the class from a published package, which no check of this one sees. A package meant to be extended should not call overridable methods while constructing at all: make them private, or abstract (which the rule always reports).

What the rule can't see ​

  • A subclass outside the program, as above.
  • Calls inside callbacks: an arrow function or function in the constructor is taken to run later (target.addEventListener("x", () => this.onX())), even when it runs right away (items.forEach(() => this.add())).
  • A method taken as a value (const f = this.describe; f()), a call through another object (helper.run(this)), and classes made by a function (extends mixin(Base)) or declared inside one.
  • A subclass's field that overwrites what the base constructor assigned (useDefineForClassFields semantics): a different bug, not a call.

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