I have a package A which is a dependency of package B.
In pkg A, I have a class-based enum defined to serve certain purpose:
export class Stage {
readonly name: string;
public static readonly PROD: Stage = new Stage('Prod');
public static readonly BETA: Stage = new Stage('Beta');
private constructor(name: string) {
this.name = name;
}
public static fromName(name: string) {
if (name == 'Prod') return Stage.PROD;
if (name == 'Beta') return Stage.BETA;
return null;
}
}
In pkg B, directly access the static field will throw up errors:
import {Stage} from "pkg-a";
const beta: Stage = Stage.fromName("Beta"); // OK
const prod: Stage = Stage.PROD; // TypeError: Cannot read property 'PROD' of undefined
I have also looked into the compiled JS code of the Stage class in the node_modules. And seems like it did export those static fields:
// tsc compiled js file
class Stage {
constructor(name) {
this.name = name;
}
}
exports.Stage = Stage;
Stage.BETA = new Stage("Beta");
Stage.PROD = new Stage("Prod");
I don't know why I am still getting errors like this.
Does anyone happen to have experience about this issue?