首页 > 解决方案 > 如何在类型脚本中为新类型继承模型类型?

问题描述

我想从打字稿中的另一个模型类型继承模型类型

导出模型需要从另一个模型继承

export type A extends B={
  a:number,
  b:String
};

export type B={
  c:string

};

它在 Visual Studio 代码中显示错误并反应 ts 编译错误

标签: reactjstypescripttypes

解决方案


类型别名不支持继承。您可以使用交集类型做类似的事情:

export type A = B & { a: number, b: string };

export type B = {
    c: string
};

或者您可以使用接口(在这种情况下,这些类型是类型别名还是接口并不重要):

export interface A extends B { a: number, b: string };

export interface B {
    c: string
};


推荐阅读