[Typescript] noImplicitOverride

Let's say you extends from a base class, you intent to override a method in base class

class BaseCmp {
  showCmp() {}
  hideCmp() {}
  helperMethod() {}
}

class MyCmp extends BaseCmp {
  // we intent to override showCmp() method
  // but we give a wrong method name
  show() {}
}

Then showCmpwould never get called because of wrong name in extended class.

 

To avoid this issue, we can use `override` keyword

class BaseCmp {
  showCmp() {}
  hideCmp() {}
  helperMethod() {}
}

class MyCmp extends BaseCmp {
  // we intent to override showCmp() method
  // but we give a wrong method name
  override showCmp() {}
}

Typescript will show error if method name cannot be found in BasedCmp

 

Let's imaging another issue that, from extends class, we don't intent to override helperMethod, but we accidently did

class BaseCmp {
  showCmp() {}
  hideCmp() {}
  helperMethod() {}
}

class MyCmp extends BaseCmp {
  override showCmp() {}
  // we don't really want override helperMethod()
  helperMethod() {}
}

To avoid this problem, we can use `noImplicitOverride: true` Once turn it on, Typescript will throw error, if you didn't add `override` to show you intentation.

posted @ 2022-11-18 15:31  Zhentiw  阅读(80)  评论(0编辑  收藏  举报