typescript d.ts 文件中的枚举
Enums in typescript d.ts file
我是 Typescript 的新手,我在 create-react-project 中使用它。我有一个名为 /typings 的文件夹,我的 tsconfig.json 文件指向该文件夹,到目前为止,我已将所有类型声明放在该文件夹中的 index.d.ts 文件中。
到目前为止一切顺利。 "type" 和 "interface" 声明似乎在我项目的所有部分都可用。我没有明确地将它们从 index.d.ts 文件中导出,也没有将它们导入到任何其他文件中。
当我声明以下内容时出现问题...
enum Gender {male, female}
当我尝试在不同的文件中使用枚举时,出现错误...
Ambient const enums are not allowed when the '--isolatedModules' flag is provided
我已经阅读了关于 SO 的其他答案,其中说您应该将枚举声明为 const,但这没有帮助。我还看到建议你应该 "export defualt undefined" 在文件的底部但是当我这样做时 none 其他类型的其他类型可用项目的。
最后,我尝试更改我的编译器选项以设置 "isolatedModules": false 但它在编译时会自动切换回 true。显然 create-react-app 会一直这样做。
我应该怎么做才能让我的 index.d.ts 文件中声明的枚举自动用于我的项目的其余部分?
枚举不能进入 d.ts
。
原因是枚举有编译输出,而其他.d.ts
如接口则没有。
例如:
enum Hello{
One = 'This is one',
Two = ''
}
编译为:
"use strict";
var Hello;
(function (Hello) {
Hello["One"] = "This is one";
Hello["Two"] = "";
})(Hello || (Hello = {}));
而接口:
interface Hello {
test: string;
}
编译为:
// an empty file, as interfaces are only used during pre-compile not in Javascript.
.d.ts
文件未显式导入文件,因此 Typescript 不知道将此文件内容注入何处。您很可能知道,Javascript 是从上到下编译的,因此将枚举注入输出 Javascript 的位置至关重要。
过去,我们将我们的枚举放入 common.ts
文件和 class,当您需要使用枚举时,我们会导入该文件。
我是 Typescript 的新手,我在 create-react-project 中使用它。我有一个名为 /typings 的文件夹,我的 tsconfig.json 文件指向该文件夹,到目前为止,我已将所有类型声明放在该文件夹中的 index.d.ts 文件中。
到目前为止一切顺利。 "type" 和 "interface" 声明似乎在我项目的所有部分都可用。我没有明确地将它们从 index.d.ts 文件中导出,也没有将它们导入到任何其他文件中。
当我声明以下内容时出现问题...
enum Gender {male, female}
当我尝试在不同的文件中使用枚举时,出现错误...
Ambient const enums are not allowed when the '--isolatedModules' flag is provided
我已经阅读了关于 SO 的其他答案,其中说您应该将枚举声明为 const,但这没有帮助。我还看到建议你应该 "export defualt undefined" 在文件的底部但是当我这样做时 none 其他类型的其他类型可用项目的。
最后,我尝试更改我的编译器选项以设置 "isolatedModules": false 但它在编译时会自动切换回 true。显然 create-react-app 会一直这样做。
我应该怎么做才能让我的 index.d.ts 文件中声明的枚举自动用于我的项目的其余部分?
枚举不能进入 d.ts
。
原因是枚举有编译输出,而其他.d.ts
如接口则没有。
例如:
enum Hello{
One = 'This is one',
Two = ''
}
编译为:
"use strict";
var Hello;
(function (Hello) {
Hello["One"] = "This is one";
Hello["Two"] = "";
})(Hello || (Hello = {}));
而接口:
interface Hello {
test: string;
}
编译为:
// an empty file, as interfaces are only used during pre-compile not in Javascript.
.d.ts
文件未显式导入文件,因此 Typescript 不知道将此文件内容注入何处。您很可能知道,Javascript 是从上到下编译的,因此将枚举注入输出 Javascript 的位置至关重要。
过去,我们将我们的枚举放入 common.ts
文件和 class,当您需要使用枚举时,我们会导入该文件。