Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 将接口导出为部分<;其他接口>;_Typescript - Fatal编程技术网

Typescript 将接口导出为部分<;其他接口>;

Typescript 将接口导出为部分<;其他接口>;,typescript,Typescript,我有一个定义如下的接口: 导出接口Iface{ id:字符串; foo:string; } 我想创建一个属于Iface的部分接口并将其导出。我试过了 导出接口partialFace=Partial; 但是typescript抱怨有部分错误。如果我将界面关键字更改为类型,它会工作。为什么我必须将它声明为类型别名而不是接口 导出类型partialFace=Partial; 请看一下: 导出接口Iface{ id:字符串; foo:string; } 导出类型PartialFace=Partial

我有一个定义如下的接口:

导出接口Iface{
id:字符串;
foo:string;
}
我想创建一个属于
Iface
的部分接口并将其导出。我试过了

导出接口partialFace=Partial;
但是typescript抱怨有部分错误。如果我将
界面
关键字更改为
类型
,它会工作。为什么我必须将它声明为
类型
别名而不是
接口

导出类型partialFace=Partial;
请看一下:

导出接口Iface{
id:字符串;
foo:string;
}
导出类型PartialFace=Partial
此外,您还可以复制粘贴界面并使所有属性成为可选:

导出接口Iface{
id?:字符串;
foo?:字符串;
}

我不知道还有其他方法

您试图将部分分配给一个不可能的接口。您可以做的是分配给其他实体作为接口的一部分

export interface Iface {
   id: string;
   name: string;
} 

const me: Partial<Iface> = {
  name: 'me-name'
 }
导出接口Iface{
id:字符串;
名称:字符串;
} 
const me:Partial={
名字:“我的名字”
}

基本上,您可以将一个类型分配给另一个类型并执行您想要执行的操作。

如果出于任何原因想使用
interface
而不是
type
,您可以
export interface partialface extensed Partial{}