override
override marks a class member that is meant to replace a member of a superclass. If the superclass has no such member, the compiler errors. noImplicitOverride requires the keyword on every override.
Detailed Explanation
Renaming a method on a base class is a quiet disaster without override. The subclass still has renderWidget(), the base now has renderView(), and nothing calls the subclass method. The program compiles. The old name is just a new unused method.
override makes the relationship explicit:
class Child extends Base {
override label(): string {
return "child";
}
}
If Base has no label, this is an error. If someone renames label on Base, every override label becomes an error, which is the list of files you have to touch.
The keyword works on methods, getters, setters, and fields that override fields. It does not work on constructors (constructor is not an override). It is erased; the emit is a normal method.
noImplicitOverride in tsconfig flips the default: overriding without the keyword is an error. That is the useful setting in a codebase that actually uses inheritance. Without the flag, override is optional and only helps the members you remembered to mark.
Compatibility rules are the usual subtype rules. The override's parameters must accept at least what the base accepts (parameter types are contravariant in the strict function-types sense for callbacks; for methods TypeScript is a bit more lenient historically). The return type must be assignable to the base return type. Tightening a return from string | undefined to string is fine. Widening it to string | number is not.
override does not call the parent. You still write super.label() if you want the parent body. Forgetting super is a logic bug, not a type error, unless you made the parent method abstract.
Use override the moment you have a second class in a hierarchy. Turn on noImplicitOverride when you turn on strict. The cost is a keyword on each override. The benefit is that a base-class rename is a compile failure instead of a production no-op.
Code Examples
1. A correct override.
// 1. A correct override.
class Job {
label(): string {
return "job";
}
}
class EmailJob extends Job {
override label(): string {
return "email-job";
}
}
console.log(new EmailJob().label()); // email-job2. A rename on the base breaks override, which is what you want.
// 2. A rename on the base breaks override, which is what you want.
class Task {
title(): string {
return "task";
}
}
class EmailTask extends Task {
// override label(): string { return "email"; }
// Error: This member cannot have an 'override' modifier because
// it is not declared in the base class 'Task'.
override title(): string {
return "email";
}
}3. Require the keyword on every override.
// 3. Require the keyword on every override.
{
"compilerOptions": {
"strict": true,
"noImplicitOverride": true
}
}Common Gotchas
`override` on a member the base does not have is a compile error
that is the feature. If you meant to add a new method, drop the keyword.
Without `noImplicitOverride`, a typo still compiles
`labell()` is a new method. The flag is what makes the keyword mandatory.
`override` does not call `super`
you must write `super.method()` yourself when you want the parent implementation.
Changing only the subclass method name without updating callers is a different bug
`override` checks the parent link, not your call sites.