首页 > 解决方案 > 打字稿运行时错误:无法读取未定义(枚举)的属性

问题描述

我在文件RESTConfig.ts中有以下接口和枚举:

export const enum RESTMethod {
   POST = "POST",
   GET = "GET"
}

export interface RESTConfig {
   url: string;
   method: RESTMethod;
   data: any;
}

我想在另一个类中导入和使用枚举:

import { RESTConfig, RESTMethod } from './RESTConfig';

class Pipelines {
   ...
   private someMethod() {
      let rest: RESTConfig = {
         url: "",
         method: RESTMethod.POST,
         data: {}
      }
      ...

   }
   ...
}

linting 和 transpiling 工作正常,但在运行时出现以下错误:

TypeError:无法读取未定义的属性“POST”

在“方法:RESTMethod.POST”行上。

有人可以告诉我我做错了什么吗?

标签: typescriptenumsundefined

解决方案


我刚刚发现,如果你有循环导入,这也可能发生。


推荐阅读