gpt4 book ai didi

typescript - 将接口(interface)导出为 Partial

转载 作者:行者123 更新时间:2023-12-05 03:47:16 26 4
gpt4 key购买 nike

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

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

我想创建一个接口(interface),它是 Iface 的一部分并将其导出。我试过了

export interface PartialIface = Partial<Iface>;

但是 typescript 提示部分错误。如果我将 interface 关键字更改为 type 它会起作用。为什么我必须将它声明为 type 别名而不是 interface

export type PartialIface = Partial<Iface>;

最佳答案

请看:

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

export type PartialFace=Partial<Iface>

您还可以复制粘贴您的界面,并将所有属性设为可选:

export interface Iface {
id?: string;
foo?: string;
}

我不知道其他方法

关于typescript - 将接口(interface)导出为 Partial<OtherInterface>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64899894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com