TS 限制类本身的基类

2021/4/11 10:29:12

本文主要是介绍TS 限制类本身的基类,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

class Base {
  prop = null;
}

class Derived extends Base {
  derived = null;
}

class Another {
  another = null;
}

function fn(someClass: { prototype: Base }) {
  someClass;
}

// ok
fn(Base);
fn(Derived);

// 类型“typeof Another”的参数不能赋给类型“{ prototype: Base; }”的参数。
//   属性“prototype”的类型不兼容。
//     类型 "Another" 中缺少属性 "prop",但类型 "Base" 中需要该属性。ts(2345)
fn(Another);


这篇关于TS 限制类本身的基类的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程