typescript - 在另一个接口(interface)中使用 Typescript 接口(in

假设我有一个 Typescript 接口(interface)如下

export interface IMyObj {
    id: string;
    type: 'AA' | 'AZ' | 'XY';
    ...
}

现在我需要另一个接口(interface),它也有那个 type 字段

export interface IMyOtherObj {
    ...
    type: 'AA' | 'AZ' | 'XY';
    ...
}

如您所见,我复制了 type 的值。所以我的问题是,如何在我的 IMyOtherObj 界面中重用 IMyObj.type?我试过了

export interface IMyOtherObj {
    ...
    type: IMyObj.type; // -> error
    ...
}

我想我很接近,但到目前为止还没有运气,有什么建议吗?

最佳答案

您的问题是 TS 类型系统没有 . 属性访问权限,但有 indexed property type , 在你的类型定义中改变一件事:

type: IMyObj['type']

关于typescript - 在另一个接口(interface)中使用 Typescript 接口(interface)的一个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60832577/

相关文章:

reactjs - Ant Design v4 卡片样式boxShadow

node.js - 从 lambda 向 sns 主题发布消息

reactjs - react Hook UseEffect 与类名

php - 自动为 laravel 模型中的字段赋值

python - 有没有办法从图形图像中提取数据并将其存储在 Excel 工作表或 csv 文件中?

azure-devops - 如何将 Yarn 注册表用作 Azure DevOps 工件上游源?

date - 如何在 DART(Flutter)中舍入最接近 30 分钟间隔的日期时间?

typescript - 类型 'Readonly

javascript - 在 Redux Saga 中使用 map 功能进行 fork

azure-devops - 创建与 Azure 资源管理器的服务连接时出错 : azure pip