I have a hard time converting a string coming from the env variable to enum.
Here's the enum:
enum Environment {
Test = 1,
Development,
Production
}
export default Environment;
And here's what I've been trying:
export default class GlobalParameters {
public static Env: Environment = Environment[<string>process.env.NODE_ENV];
}
console.log(process.env.NODE_ENV) // Gives "Development"
let str = String(process.env.NODE_ENV); // Gives "Development"
console.log(Environment[str]) //Gives undefined
Object.seal(GlobalParameters);
Your code seems to work fine:
enum Environment {
Test = 1,
Development,
Production
}
console.log(Environment[2]) // "Development"
let str = String(Environment[2]);
console.log(str); // "Development"
console.log(Environment[str]) // 2
Nevermind, when I was defining the environment variable in the command line it must have added some whitespace characters, because when I changed retrieving string to
let str: string = String(process.env.NODE_ENV).replace(" ", "");
It works fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.