简体   繁体   English

编译时未定义 TypeScript 枚举

[英]Undefined TypeScript enum at compile time

I have an enum defined in types.ts :我在types.ts中定义了一个枚举:

export enum Handedness {
  Left,
  Right,
  Both,
}

export type State = {
  count: number
  handedness: Handedness
}

and I have an object being initialized in state.ts :我在 state.ts 中初始化了一个state.ts

import { State, Handedness } from './types'

export const initial: State = {
  count: 0,
  handedness: Handedness.Both
}

When I run tests (via jest ) for this project, state.ts generates an error TypeError: Cannot read property 'Both' of undefined , telling me that Handedness isn't defined at the time it's referenced.当我为此项目运行测试(通过jest )时, state.ts生成错误TypeError: Cannot read property 'Both' of undefined ,告诉我Handedness在引用时未定义。 But I'm exporting it from its module and importing it before I use it... so it should be defined.但是我在使用它之前从它的模块中导出它并导入它......所以它应该被定义。

I've found other similar questions asking about undefined enums, but they seem to all be asking about runtime.我发现其他类似的问题询问未定义的枚举,但他们似乎都在询问运行时。 This is a compile time problem as far as I can tell.据我所知,这是一个编译时问题。

I don't see what I would be doing wrong here.我不明白我会在这里做错什么。 I import other types in other places without issue.我在其他地方导入其他类型没有问题。 But this enum simply doesn't want to work.但是这个枚举根本不想工作。 What is going on here and how can I work around it?这是怎么回事,我该如何解决?

Well, this isn't a way to get this to work, but this GitHub PR explains that ts-jest won't support enums that work like this.好吧,这不是让它工作的方法,但是这个 GitHub PR解释说ts-jest不支持像这样工作的枚举。 I've changed all uses to (eg) ("both" as Handedness) and it works.我已将所有用途更改为(例如) ("both" as Handedness)并且它有效。 So, that's not an explanation, it's a workaround.所以,这不是解释,而是一种解决方法。

You can also try using const enums .您也可以尝试使用const enums

So change:所以改变:

export enum Whatever { ... }

to

export const enum Whatever { ... }

Apparently as of ts-jest version 23.10 you can just do this. 显然,从ts-jest版本 23.10 开始,您就可以这样做。 Much better!好多了!

I encountered similar error while running my code with Jest.我在用 Jest 运行我的代码时遇到了类似的错误。 The issue was caused by a React component that imported an enum type from a module which was also being mocked with jest.mock.该问题是由 React 组件从模块中导入枚举类型引起的,该模块也被 jest.mock 模拟。 I fixed the issue by using jest.spyOn for method that I needed instead of whole module.我通过将 jest.spyOn 用于我需要的方法而不是整个模块来解决了这个问题。

from: jest.mock("example/path", () => ({ useExampleMethod: () => {...来自: jest.mock("example/path", () => ({ useExampleMethod: () => {...

to: import * as exampleModule from "example/path"; jest.spyOn(exampleModule, 'useExampleMethod').mockReturnValue({... to: import * as exampleModule from "example/path"; jest.spyOn(exampleModule, 'useExampleMethod').mockReturnValue({... import * as exampleModule from "example/path"; jest.spyOn(exampleModule, 'useExampleMethod').mockReturnValue({...

Similar issue here, but a slightly different cause & solution.此处存在类似问题,但原因和解决方案略有不同。 Posting here in hopes that this helps others.在这里发帖希望对其他人有所帮助。

  1. I was defining my enum in a file /auth/types.ts :我在文件/auth/types.ts中定义我的枚举:
....
export enum SampleEnum {
    FIRST = 1,
    SECOND = 2
}
  1. I was then exporting my types en-masse from /auth/index.ts :然后我从/auth/index.ts导出我的类型:
export * from './types;
  1. Finally, I was importing this from the parent directory elsewhere in the app:最后,我从应用程序其他地方的父目录导入它:
import { SampleEnum } from './auth'
...

const someOtherObject = {
  place: SampleEnum.FIRST
}

Running Jest, I was getting the TypeError: Cannot read property 'FIRST' of undefined error, even when the code compiled just fine.运行 Jest,我得到了TypeError: Cannot read property 'FIRST' of undefined错误,即使代码编译得很好。

To solve this, I switched my import statement to为了解决这个问题,我将导入语句切换为

import { SampleEnum } from './auth/types'

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

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