TS Dynamic Attribute
index type
class Foo { hello = "hello"; world = 1234; // This is an index signature: [propName: string]: string | number | undefined; } let instance = new Foo(); // Valid assigment instance["whatever"] = 42; // Has type 'string | number | undefined'. let x = instance["something"];
Common errors
An index signature parameter type cannot be a literal type or generic type. Consider using a mapped object type insteadclass Foo { static hello = "hello"; static world = 1234; static [propName: string]: string | number | undefined; } // Valid. Foo["whatever"] = 42; // Has type 'string | number | undefined' let x = Foo["something"];