I am attempting to use Javarome's answer to a previous TypeScript question regarding how to use an object in an enum:
class Material {
public static readonly ACRYLIC = new Material(`ACRYLIC`, `AC`, `Acrylic`);
public static readonly ALUM = new Material(`ALUM`, `AL`, `Aluminum`);
public static readonly CORK = new Material(`CORK`, `CO`, `Cork`);
public static readonly FOAM = new Material(`FOAM`, `FO`, `Foam`);
// private to diallow creating other instances of this type.
private constructor(
public readonly key: string,
public readonly id: string,
public readonly name: string
) {}
public toString(): string {
return this.key;
}
}
Unfortunately, I am running into an issue later in the code when I attempt to use bracket-syntax (because it's in a for-of
loop):
const materials: string[] = [`ACRYLIC`, `FOAM`];
for (const materialKey of materialsArray) {
const material: Material = Material[materialKey];
// ...
}
That pops up a huge TS error [TS(7053)] with the following message:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'typeof Material'.
No index signature with a parameter of type 'string' was found on type 'typeof Material'.ts(7053)
I have been Googling for hours but have found nothing that helps. Is there any way to reference this "enum" using bracket-syntax?