如何在 Typescript 中将类型转换为接口

How to convert type to interface in Typescript

是否可以将类型化对象转换为 TypeScript 中的接口?

I already read , and don't think it fits well with the description I give in this Question.

快速示例,此场景中 type Product 被定义为来自第三方 TypeScript 库的 type

# ProductFragmentOne is used to highligt possibility of composition(unions, etc)
type Product = ProductFragmentOne & { sku: string };

要将该产品集成到我们自己的系统中,已经可以通过扩展(联合)一个类型来实现,如下例所示:

export type ProductSchema = Product & { 
  name: string;
}

我的问题是:

# Example of how the code may look
export interface ProductSchema{ 
  name: string;
  // do the magic to add Product properties here
}

更新: 采用这种方法的原因纯粹是接口优先于类型。也使现有代码保持其风格,无论采用的第三方库如何。

谢谢。

为了解决这个问题,我使用了在一个完全不相关的问题上找到的答案:Is it .

事实上,从 TypeScript 2.2 开始——interface 可以扩展 type

There is an extensive thread on the difference between interface and type on this Whosebug thread:

export interface ProductSchema extends Product{ 
  name: string;
}
# Where the Product is a type similar to
type Product = { SKU: string }

我正在寻找的“魔术”确实是 extends 运算符(或关键字)。 TypeScript playground based on this same approach 还有一个例子