gpt4 book ai didi

javascript - 使用@babel/preset-typescript 从环境 TypeScript 模块中使用枚举?

转载 作者:行者123 更新时间:2023-12-05 02:14:15 25 4
gpt4 key购买 nike

是否可以使用 @babel/preset-typescript 从正在由 babel 转译的文件中使用 enum

mymodule.d.ts

declare module 'mymodule' {
export enum Fruit {
apple = 'Apple',
}
}

script.js

import { Fruit } from 'mymodule'

assert.equals(Fruit.apple === 'Apple')

Fruit 在这种情况下将是未定义的,因为 babel 不知道环境声明。

除了声明一个单独的枚举/常量文件并直接在两个地方(环境模块和脚本)导入它们之外,还有什么办法可以解决这个问题吗?

最佳答案

您不能从声明文件 (*.d.ts) 导入可执行代码。

您可以使用声明文件来描述另一个模块中发生的事情。在这种情况下,如果 Fruit 存在于 mymodule 中,您可以在 mymodule.d.ts 中声明它的形状——但它不是被执行的声明,但实际代码存在于 mymodule 中。

换句话说,Fruit 必须存在于*.ts*.js 文件中才能在运行时使用。

关于javascript - 使用@babel/preset-typescript 从环境 TypeScript 模块中使用枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53768978/

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