TypeScript 中的接口、类与泛型:深入解析
1. 构造函数类型不兼容问题
在 TypeScript 中,当使用构造函数时,构造函数的返回类型会被 TypeScript 编译器隐式类型化。例如,若有 ComplexType 和 IComplexType ,尽管 ComplexType 函数实现了 IComplexType 接口,但它们实际上是不同的类型,构造函数签名会不兼容,编译器会产生如下错误:
Types of property 'constructor' of types 'ComplexType' and 'IComplexType' are incompatible
2. 继承
继承是面向对象编程的基石之一,它允许一个对象使用另一个对象作为其基类型,从而“继承”基对象的所有特征,包括属性和函数。接口和类都可以使用继承,使用 extends 关键字实现。
2.1 接口继承
以下是接口继承的示例代码:
interface IBase {
id: number;
}
interface IDerivedFromBase extends IBase {
name: string;
}
class DerivedClass implements IDerivedFromB
订阅专栏 解锁全文
3

被折叠的 条评论
为什么被折叠?



