Typescript runtime error: cannot read property of undefined (enum)

Yuri van Geffen picture Yuri van Geffen · May 16, 2018 · Viewed 7k times · Source

I have the following interface and enum in a file RESTConfig.ts:

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

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

I want to import and use the enum in another class as such:

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

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

   }
   ...
}

Linting and transpiling works fine, but at runtime I get the following error:

TypeError: Cannot read property 'POST' of undefined

on the line "method: RESTMethod.POST".

Can someone tell me what I'm doing wrong?

Answer

Christiaan Maks picture Christiaan Maks · Jan 9, 2020

I just found out the hard way that this can also happen if you have circular imports.