首页 > 其他分享 >[Typescript] noImplicitOverride

[Typescript] noImplicitOverride

时间:2022-11-18 15:49:10浏览次数:44  
标签:Typescript noImplicitOverride BaseCmp showCmp method helperMethod override class

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.

标签:Typescript,noImplicitOverride,BaseCmp,showCmp,method,helperMethod,override,class
From: https://www.cnblogs.com/Answer1215/p/16903424.html

相关文章

  • 【草稿】在 Typescript 中从对象中动态解构接口类型
    问题interfaceA{title:string,description:string,}leta={title:"titlea",description:"descriptiona",url:"http://example.com/a"}是......
  • React中常见的TypeScript定义实战
    一引沿Fiber架构是React16中引入的新概念,目的就是解决大型React应用卡顿,React在遍历更新每一个节点的时候都不是用的真实DOM,都是采用虚拟DOM,所以可以理解成fiber就是R......
  • React-hooks+TypeScript最佳实战
    ReactHooks什么是HooksReact一直都提倡使用函数组件,但是有时候需要使用state或者其他一些功能时,只能使用类组件,因为函数组件没有实例,没有生命周期函数,只有类组件才......
  • 自学 TypeScript 第三天 使用webpack打包 TS 代码
    前言:大家好啊,昨天介绍了TS编译器的配置,但在我们实际开发当中直接使用TS编译器去编译代码的情况会有,但没有很多,因为我们在开发大型项目的时候,一般我们都会用到打包工具......
  • 这10个TypeScript高级技巧,助你成为更好的开发者!
    在使用了一段时间的Typescript之后,我深深地感受到了Typescript在大中型项目中的必要性。可以提前避免很多编译期的bug,比如烦人的拼写问题。并且越来越多的包都在使用TS,所以......
  • [Typescript] Variable assignment - extends infer X
    Youcanuse`extendsinferX`toassigntheresultofanexpressiontoavariabletypeSomeFunction<U>=SuperHeavyComputation<U>extendsinferResult......
  • 自学 TypeScript 第二天 编译选项
    前言:昨天我们学习了TS的数据类型,不知道大家回去以后练习没练习,如果你练习了一定会发现一个问题,我们的TS好像和JS不太一样JS写完之后直接就可以放到页面上,就可以用......
  • NOTE_vanilla+typescript
    E:\song\threejs_learn\vite-project\index.html<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"/><linkrel="icon"type="image/svg+xml"......
  • [Typescript]106. Medium - OnPropChnagedMethods
    typeOnPropChnagedMethods<T>={[KeyinkeyofT&stringas`${Key}Changed`]:(cb:(newValue:T[Key])=>void)=>void}declarefunctionmakeWatchedObject<......
  • [Typescript] 105. Medium - Placeholder
    typePlaceholder<Textendsstring>=Textends`${string}{${inferP}}${inferREST}`?P|Placeholder<REST>:never;declarefunctionformat<Sextendsstr......