Class incorrectly extends base class
A derived class incorrectly extends a base class, often because of incompatible property types or access modifiers.
- 1A property in the derived class has a type that is not assignable to the same property in the base class.
- 2Overriding a public property with a private one.
- 3The constructor of the derived class does not correctly call 'super()' when required.
A subclass overrides a property with an incompatible type.
class Animal {
name: string;
}
class Dog extends Animal {
name: number; // Incompatible type
}expected output
error TS2415: Class 'Dog' incorrectly extends base class 'Animal'.
Types of property 'name' are incompatible.
Type 'number' is not assignable to type 'string'.Fix 1
Ensure property types are compatible
WHEN Property types conflict.
class Animal {
name: string;
}
class Dog extends Animal {
name: string; // Correct type
}Why this works
The subclass property type must be assignable to the base class property type.
Fix 2
Use a different property name in the subclass
WHEN The subclass property is unrelated to the base class property.
class Animal {
name: string;
}
class Dog extends Animal {
dogTagId: number;
}Why this works
Avoiding a name collision prevents the type conflict.
✕ Use 'any' as the type in the subclass
This defeats the purpose of type checking and can hide significant bugs in your class hierarchy.
microsoft/TypeScript src/compiler/diagnosticMessages.json
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev