Can i initialize static readonly class field with process.env.FOO in Express using Typescript?

Viewed 163

Is it possible to initialize static readonly class field with env variable in Express using Typescript? I use .env file.

My code currently looks like this:

class Foo {
  private static readonly port: number = process.env.PORT!;

  public static getStaticPort() {
    return this.port; // undefined
  }

  public static getProcessPort() {
    return process.env.PORT; // 42
  }
}

When i call Foo.getStaticPort() it gives me undefined.

0 Answers
Related