gpt4 book ai didi

typescript - 将枚举导入接口(interface)中断接口(interface)

转载 作者:搜寻专家 更新时间:2023-10-30 21:27:41 25 4
gpt4 key购买 nike

我想将枚举导入接口(interface)。 (使用 typescript 2.5)但这会破坏在另一个界面中使用该界面。这是示例代码


allEnums.ts

export enum ButtonType {
Top = 1,
Bottom = 2
}

other enums following ...

按钮界面.d.ts

import { ButtonType } from "allEnums";
interface ButtonInterface {
buttonType: ButtonType
}

formInterface.d.ts

interface FormInterface { 
buttos: ButtonInterface[]
}

结果是 formInterface.d.ts 中的一个错误

Cannot find name ButtonInterface


有帮助的是像这样将 ButtonInterface 导入 FormInterface

import { ButtonInterface } from "buttonInterface";

但我认为导入接口(interface)不是一个好的解决方案

最佳答案

截至TypeScript 2.9你可以导入一个类型而不导入它包含的模块:

import("./buttonInterface").ButtonInterface

只要您使用的是 TypeScript 2.9 或更高版本,就可以在您的场景中使用它。你也可以给它一个别名:

type ButtonInterface = import('./buttonInterface').ButtonInterface;

interface FormInterface {
buttos: ButtonInterface[]
}

您的 IDE/文本编辑器可能无法满足此要求,但如果您看到编辑器中的错误,请运行 tsc 进行检查。

关于typescript - 将枚举导入接口(interface)中断接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50694569/

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