Abstract Class Constructor Should Be Protected

ID

javascript.ts_abstract_constructor_public

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Best Practice

Language

JavaScript

Tags

best-practice, classes, typescript

Description

Reports an abstract class whose constructor is left implicitly public, or marked public explicitly:

abstract class Base {
  constructor(name: string) { ... }            // FLAW — implicitly public
}

abstract class Base {
  public constructor(name: string) { ... }     // FLAW — explicitly public
}

Rationale

An abstract class cannot be instantiated directly — any caller that writes new Base(…​) gets a TypeScript compile error. A public constructor on such a class is therefore actively misleading: it implies a contract the class does not provide.

Marking the constructor protected is the idiomatic alternative. It documents that the constructor is intended to be invoked from subclasses (via super(…​)) and matches the way the language already restricts the call.

Remediation

Add the protected modifier:

abstract class Base {
  protected constructor(name: string) { ... }
}

class Concrete extends Base {
  constructor() { super("c"); }
}

If the class is meant to be instantiable, drop the abstract modifier instead.

Notes

The rule fires only on classes declared with the abstract modifier. The constructor’s access is detected through the surrounding class-element wrapper that TypeScript adds for any access modifier — public, protected, private. A constructor with no explicit modifier counts as public for the purpose of this rule.