TS Index Signature

Creator
Creator
Seonglae Cho
Created
Created
2021 Jun 5 12:21
Editor
Edited
Edited
2023 Sep 13 12:28
Refs
Refs

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 instead
class 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"];
 
 
 
 
 
 

Recommendations