简体   繁体   English

类型在 TypeScript 中定义一个枚举

[英]Type define an Enum in TypeScript

I have a TypeScript Enum that lives within a different namespace to the file I am trying to use it in. The namespace for now unavoidable reasons is long and cumbersome to write我有一个 TypeScript 枚举,它与我尝试使用它的文件位于不同的命名空间中。由于现在不可避免的原因,命名空间很长而且编写起来很麻烦

The project is not using modules and does not have a module loader so no imports or exports.该项目没有使用模块,也没有模块加载器,因此没有导入或导出。 My hands are tied here unfortunately.不幸的是,我的双手被绑在这里。 We bundle files manually.我们手动捆绑文件。

enum Movement {
    run,
    walk
}
function Move(movement: App.System.User.Area.Movement) { 
    if (movement === App.System.User.Area.Movement.run) { //... }
    //....
}

I am able to use it seems the type keyword to (and I am not sure this is the TypeScript word for it) type def that long namespace away.我可以使用它似乎type关键字(我不确定这是 TypeScript 字)类型 def 那个长命名空间。

type MovementType = App.System.User.Area.Movement;
function Move(movement: MovementType) { 
    if (movement === App.System.User.Area.Movement.run) { //... }
    //....
}

But I cannot use that type def'd type in the equals comparison in my function above because "MovementType only refers to a type but is being used as a value here" when I try to do:但是我不能在上面的 function 的等于比较中使用该类型定义类型,因为当我尝试这样做时,“MovementType 仅指一种类型,但在此处用作值”:

type MovementType = App.System.User.Area.Movement;
function Move(movement: MovementType) { 
    if (movement === MovementType.run) { //... }
    //....
}

Is there any way to get around this?有没有办法解决这个问题? Why can't I use it in the conditional statement while I can have it as a parameter?为什么我不能在条件语句中使用它,而我可以将它作为参数? How can I get around my very long namespace?我怎样才能绕过我很长的命名空间?


I am currently using TypeScript 3.1我目前正在使用 TypeScript 3.1

As Pac0 mentioned, an enum is both a set of values and a type.正如 Pac0 提到的,枚举既是一组值又是一个类型。 If the import solution doesn't work for you, you could try aliasing both the type and value.如果导入解决方案不适合您,您可以尝试为类型和值设置别名。

type MovementType = App.System.User.Area.Movement;
const Movement = App.System.User.Area.Movement;
function Move(movement: MovementType) { 
    if (movement === Movement.run) { //... }
    //....
}

Example on TypeScript Playground TypeScript 操场上的示例

The type keyword is indeed only creating an alias for the type that is defined by the enum , it can't work to access the values of the enum (hence, the explicit error messages). type关键字确实只是为enum定义的类型创建别名,它不能访问 enum 的(因此,显式错误消息)。

You can achieve something similar to what you want by aliasing the long namespace with import :您可以通过使用import为 long 命名空间起别名来实现与您想要的类似的东西:

import area = App.System.User.Area;

function Move(movement: area.Movement) { 
    if (movement === area.Movement.run) { //... }
    //....
}

That should at least help a bit.这至少应该有所帮助。

More info in TypeScript docs . TypeScript 文档中的更多信息。

Example on TypeScript playground TypeScript 操场上的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM