TypeScript Reference ยท Keywords and Declarations
TypeScript override modifier
Reference for the TypeScript override modifier: marking members that replace a base class member, the noImplicitOverride option, errors and refactoring safety.
override marks a class member as intentionally replacing a member of the base class. The compiler then checks that the base class really has a member with that name. If someone renames or removes the base method later, every override that no longer matches becomes an error instead of silently turning into a new, never-called method.
Used together with the noImplicitOverride compiler option, it also forces you to write override whenever you do replace something, so overriding is never accidental.
class Base { hello() { return "hi"; } } class Derived extends Base { override hello() { return "hello"; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
class D extends B { override method(): void {} override field = value; override get prop(): T { /* ... */ } constructor(override readonly id: number) { super(); } // parameter property }
| Part | Description |
|---|---|
override |
Asserts that a member with the same name exists in the base class. Allowed on methods, properties, accessors and parameter properties. |
| Ordering | Goes after visibility and static/abstract, before readonly: protected override readonly x. |
noImplicitOverride |
When on, a member that overrides a base member without override is an error. Not part of strict; enable it yourself. |
Errors
- No such base member: error TS4113 ("This member cannot have an 'override' modifier because it is not declared in the base class 'Base'").
- Using
overridein a class with noextends: error TS4112 ("... its containing class 'Plain' does not extend another class"). - Missing modifier under
noImplicitOverride: error TS4114 ("This member must have an 'override' modifier because it overrides a member in the base class").
Usage notes
- Pure compile-time.
overrideis erased, with no runtime effect. - Suggested setup. Turn on
noImplicitOverridefor new code bases; migration is mechanical (add the keyword where errors are reported). - Signature compatibility is still enforced: an override must be assignable to the base member (error TS2416 otherwise).
- Interfaces do not count. Implementing an interface member needs no
override, becauseimplementshas no base implementation to replace. - Fields. An overridden field with a new initializer replaces the value after
super()returns; withuseDefineForClassFieldsit also redefines the property.