TS Index Signature

Creator
Creator
Seonglae ChoSeonglae 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"];
 
 
 
 
Announcing TypeScript 4.3 | TypeScript
Today we're excited to announce the availability of TypeScript 4.3! If you're not yet familiar with TypeScript, it's a language that builds on JavaScript by adding syntax for static types. Tools like the TypeScript compiler can just erase TypeScript syntax, leaving you with clean readable JavaScript that works anywhere.
Announcing TypeScript 4.3 | TypeScript
 
 

Recommendations