how to use string indexed interface of typescript? how to use string indexed interface of typescript? arrays arrays

how to use string indexed interface of typescript?


Using the indexer limits what can be put or fetched for the object using the index syntax. E.g. foo is inferred to be of type string:

interface IDictionary {     [index: string]: string;}var params = {} as IDictionary;params['heart'] = 'HP'; // okayvar foo = params['heart']; // foo:string

The following on the other hand is an error as it will not type check:

var bar:number = params['heart']; // ERRORparams['heart'] = 234; // ERROR

Complete demo:

interface IDictionary {     [index: string]: string;}var params = {} as IDictionary;params['heart'] = 'HP'; // okayvar foo = params['heart']; // foo:stringvar bar:number = params['heart']; // ERRORparams['heart'] = 234; // ERROR