Search code examples
angulartypescriptdeclareanyreferenceerror

Angular2 Typescript declare variable any throws ReferenceError


Hi StackOverflow community,

I have JavaScript code outside which set global js variable, but in some cases this variable does not exist. I have configuration class in Angular2 which import that global variable via declare var any. In the class I have static variable with ternary operator which checks if this global variable exists (!=null) then set some statement but it throws: Uncaught ReferenceError: IMPORTED_VAR is not defined. How to solve it?

declare var IMPORTED_VAR:any;
export class ConfigurationClass {
  public static CONF_VAR = IMPORTED_VAR ? IMPORTED_VAR : "SOMETHING";
}

Solution

  • Finally I created static constructor in typescript with handling this exception