Typescript 枚举,用于实现和接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37108465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript enum, used from implementation and interfaces
提问by J?rgen Tvedt
I have the following, in CommandEnum.ts:
我在 CommandEnum.ts 中有以下内容:
export enum CommandEnum {
createProject,
renameProject,
hablaBabla
}
in a module which I am able to reference from implementation code, using
在我能够从实现代码中引用的模块中,使用
import {CommandEnum} from '../server/contracts/CommandEnum'
let x = CommmandEnum.hablaBabla
The enum file is compiled into a javascript function with export logic, in CommandEnum.js.
枚举文件在 CommandEnum.js 中编译为带有导出逻辑的 javascript 函数。
This now works fine, but I want to reference this enum in my interfaces as well, I try:
这现在工作正常,但我也想在我的接口中引用这个枚举,我尝试:
/// <reference path="../contracts/CommandEnum.ts" />
namespace ValueTypes {
export interface Command {
type : CommandEnum;
referenceId : string;
}
}
Now, this reference does not import the CommandEnum type, but some of the other combinations of modules / namespace / export default I have tried does. I can get the reference syntax to work, but not the module syntax and the other way around - but not both.
现在,此参考不导入 CommandEnum 类型,但我尝试过的模块/命名空间/导出默认值的其他一些组合可以。 我可以使用参考语法,但不能使用模块语法,反之亦然 - 但不能同时使用。
Is this actually possible? Using an enum from a pure definitions interface file seems like a very common scenario. But when the interface is implemented the enum must be available in "function form" and these two models does not seem to combine?
这真的可能吗?使用来自纯定义接口文件的枚举似乎是一个非常常见的场景。但是当实现接口时,枚举必须以“函数形式”提供,而这两种模型似乎没有结合?
I had the same problem with classes, which I wanted to namespace, .Net-style - which I had to give up. Classes, however, are not referenced in my interfaces - enums are.
我对类有同样的问题,我想命名空间,.Net 风格 - 我不得不放弃。但是,我的接口中没有引用类 - 枚举是。
I work with node.js and compile to individual files, not a single concated output.
我使用 node.js 并编译为单个文件,而不是单个连接输出。
回答by basarat
This now works fine, but I want to reference this enum in my interfaces as well
这现在工作正常,但我也想在我的接口中引用这个枚举
You can move stuff from a module
into the global namespace use declare global
您可以将东西从 a 移动module
到全局命名空间 usedeclare global
E.g. myEnumGlobalDeclare.ts
例如 myEnumGlobalDeclare.ts
import {MyEnum as MyEnumModule} from "./myEnum";
declare global {
declare var MyEnum: typeof MyEnumModule;
}
E.g. myEnumGlobalDefine.ts
例如 myEnumGlobalDefine.ts
import {MyEnum as MyEnumModule} from "./myEnum";
MyEnum = MyEnumModule;
Or something similar ^. Of course this means your runtime should support globalaugmentation e.g. in nodejs you need to use globals
and in browsers window
.
或类似的东西^。当然,这意味着您的运行时应该支持全局增强,例如在您需要使用的 nodejsglobals
和浏览器中window
。
More
更多的
I definitely do not recommend going down this path. Instead create a global types.ts
module and just use that everywhere. E.g. alm has this file : https://github.com/alm-tools/alm/blob/master/src/common/types.ts
我绝对不建议走这条路。而是创建一个全局types.ts
模块并在任何地方使用它。例如 alm 有这个文件:https: //github.com/alm-tools/alm/blob/master/src/common/types.ts