basarat
basarat

Reputation: 275977

Give error on overriding functions by mistake

When I inherit a class I don't want to override a base class method by mistake e.g.

class SomeComponent {
  show() {
    // ...
  }
}
class SpecializedComponent extends SomeComponent {
  show() { // Error here as its overriding
    // ...
  }
}

Is there a way to catch unexpected overrides and force some of annotation for intentional overrides?

Upvotes: 0

Views: 183

Answers (1)

basarat
basarat

Reputation: 275977

Starting with TypeScript 4.3 there is a new --noImplicitOverride compiler option that you can set to give an error of unintended overrides.

class SomeComponent {
  show() {
    // ...
  }
}
class SpecializedComponent extends SomeComponent {
  show() { // Error ❌ !
    // ...
  }
}

If you want to override intentionally you can use the override keyword:

class SomeComponent {
  show() {
    // ...
  }
}
class SpecializedComponent extends SomeComponent {
  override show() { // All good 🥳
    // ...
  }
}

More

This a very long standing and popular request : https://www.youtube.com/watch?v=arjrOdT73b4 ❤️

Upvotes: 1

Related Questions