Index Signatures
let foo: any = {};
foo['Hello'] = 'World';
console.log(foo['Hello']); // Worldclass Foo {
constructor(public message: string){};
log(){
console.log(this.message)
}
}
let foo: any = {};
foo['Hello'] = new Foo('World');
foo['Hello'].log(); // Worldlet obj = {
toString(){
console.log('toString called')
return 'Hello'
}
}
let foo: any = {};
foo[obj] = 'World'; // toString called
console.log(foo[obj]); // toString called, World
console.log(foo['Hello']); // WorldTypeScript Index Signature
Declaring an index signature
All members must conform to the string index signature
string index signatureUsing a limited set of string literals
Having both string and number indexers
string and number indexersDesign Pattern: Nested index signature
Excluding certain properties from the index signature
Last updated