typescript 运行时未定义枚举类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47418124/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:03:25  来源:igfitidea点击:

Enum type not defined at runtime

typescript

提问by Thijs Koerselman

I have a problem where the Typescript compiler compiles my code successfully, yet the runtime gives me undefined type errors.

我有一个问题,Typescript 编译器成功编译了我的代码,但运行时却给了我未定义的类型错误。

In my app I created a types.tsfile with some things shared between multiple other ts files. It contains a string enum like:

在我的应用程序中,我创建了一个types.ts文件,其中包含在多个其他 ts 文件之间共享的一些内容。它包含一个字符串枚举,如:

enum MyEnum {
  One = "one";
  Two = "two";
}

When I define it like this. The compiler lets me use it in other ts files, and appears to be happy. However, at runtime I get the error "MyEnum is not defined".

当我这样定义它时。编译器让我在其他 ts 文件中使用它,看起来很高兴。但是,在运行时我收到错误“MyEnum 未定义”。

I know of two ways to solve this:

我知道有两种方法可以解决这个问题:

  1. Define the enum in the file where it is used. But I don't think this will solve anything for other files that want to use it.
  2. Use "export" in the types.ts file, and import every type explicitly everywhere it is used.
  1. 在使用它的文件中定义枚举。但我认为这不会为其他想要使用它的文件解决任何问题。
  2. 在 types.ts 文件中使用“export”,并在使用它的任何地方明确导入每种类型。

I am quite new to Typescript, and I feel I might be misunderstanding something fundamental.

我对打字稿很陌生,我觉得我可能误解了一些基本的东西。

First, I don't get why the Typescript compiler happily compiles my code if there's going to be a runtime error. I would understand it if I had used the declarekeyword, telling the compiler that something shouldbe available at runtime, but in this case I don't see why it should assume that the enum comes from anywhere else then the types.ts file.

首先,如果出现运行时错误,我不明白为什么 Typescript 编译器会愉快地编译我的代码。如果我使用了declare关键字,我会理解它,告诉编译器某些东西应该在运行时可用,但在这种情况下,我不明白为什么它应该假设枚举来自 types.ts 文件之外的任何其他地方。

Second, I would like to define types somewhere globally in my app and have them be available everywhere without having to import them every time I used them. How do I accomplish this? Or is this maybe considered bad practice?

其次,我想在我的应用程序中的某个地方全局定义类型,并让它们随处可用,而不必每次使用它们时都导入它们。我该如何实现?或者这可能被认为是不好的做法?

I am using Typescript 2.6 and my config looks like this:

我正在使用 Typescript 2.6,我的配置如下所示:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6",
    "module": "commonjs",
    "lib": ["es6", "es7", "esnext"],

    "sourceMap": true /* Generates corresponding '.map' file. */,
    "outDir": "build" /* Redirect output structure to the directory. */,
    "removeComments": true /* Do not emit comments to output. */,

    /* Strict Type-Checking Options */
    "strict": true /* Enable all strict type-checking options. */,

    /* Additional Checks */
    "noUnusedLocals": true /* Report errors on unused locals. */,
    "noUnusedParameters": true /* Report errors on unused parameters. */,
    "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
    "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,

    "plugins": [{ "name": "tslint-language-service" }],
    "skipLibCheck": true // because firebase-sdk has wrong type files now (Nov 18)
  },
  "include": ["src/**/*"],
  "exclude": ["build"]
}

采纳答案by Matt B

There's another way you can do this. If you don't want to export your enum you can define it as a const enum

还有另一种方法可以做到这一点。如果您不想导出枚举,可以将其定义为const 枚举

const enum MyEnum {
   One = "one";
   Two = "two";
}

These are inlined by the compiler and are completely removed during compilation.

这些由编译器内联,并在编译期间完全删除。

回答by keos

In my case of undefinedenum, it turned out it's because of circular import:

在我的undefined枚举情况下,结果是因为循环导入:

export enum A {...}defined in file a.ts, export const b = ...defined in file b.ts;

export enum A {...}在文件a.tsexport const b = ...定义,在文件中 定义b.ts

import {A} from './a.ts'in b.ts, while import {b} from './b.ts'in a.ts.

import {A} from './a.ts'b.ts,而import {b} from './b.ts'a.ts

The error was gone after removing circular imports.

删除循环导入后错误消失了。

回答by Chintsu

I had the same problem when imported re-exported enum. It caused runtime error.

导入重新导出枚举时,我遇到了同样的问题。它导致运行时错误。

layout.ts

布局.ts

export enum Part { Column, Row }

index.ts

索引.ts

export * from './layout'

component.ts

组件.ts

import { Part  } from '../entities' // This causes error
import { Part } from '../entities/layout' // This works

回答by Ciaran Gallagher

I had this error and it went away as soon as I used the export keyword, i.e.

我遇到了这个错误,一旦我使用了 export 关键字,它就消失了,即

export enum MyEnum {
  One = "one";
  Two = "two";
}

And make sure you import it in the files where you are using it as well, i.e.

并确保将它导入到您正在使用它的文件中,即

import { MyEnum } from '../types.ts';

I found that when I declared the enum without the export keyword, I could still reference the enum without importing it in other files without a compiler error - it was only at runtime the undefined exception was then thrown.

我发现,当我在没有 export 关键字的情况下声明枚举时,我仍然可以引用枚举,而无需将其导入到其他文件中,而不会出现编译器错误 - 只有在运行时才会抛出未定义的异常。

回答by Baki

For us it turned out that simply restarting the application solved the problem. (Nativescript app)

对我们来说,结果证明只需重新启动应用程序即可解决问题。(本机脚本应用程序)

回答by Ranjeet Kumar

export enum Expertlavel {
    Beginner = 0,
    Intermediate = 10,
    Expert = 20
};

Exporting all enum classes

导出所有枚举类

export * from "../Enums/enums";