class __Constants__ {
[key: string]: string;
constructor(values: string[]) {
values.forEach((key) => {
this[key] = key;
});
}
}
const Constants = __Constants__ as {
new <T extends readonly string[]>(values: T): { [k in T[number]]: k };
};
const __colors__ = ["BLUE", "GREEN"] as const;
const Colors = new Constants(__colors__);
// const Colors: {
// BLUE: "BLUE",
// GREEN: "GREEN",
// }
Is there a way to define the __Constants__
class to have the same output type as the cast, without needing to modify the custom constructor signature?
Note that I am currently working on transitioning an old JavaScript codebase to TypeScript and aiming to minimize alterations to the original JS code. The actual Constants
class is more complex, but here I've simplified the issue I'm encountering.