donnut
donnut

Reputation: 720

typescript: access property with dot notation using 'dictionary' type

Trying to access a property of dict with dot notation makes Typescript complain. The language specification, 4.10, states:

ObjExpr [ IndexExpr]

... if ObjExpr ’s apparent type has a string index signature and IndexExpr is of type Any, the String or Number primitive type, or an enum type, the property access is of the type of that index signature.

I am using:

interface MapStringToFunction {
  [index: string]: Function;
}

var dict: MapStringToFunction = {};

dict.say = () => 'hi';

dict.say();

MapStringToFunction has a sting index signature and say is of type String, so it should be allowed? But it obvious is not. What is my mistake and how can I change to code so I can type dict and access properties with dot notation?

Upvotes: 7

Views: 5012

Answers (1)

basarat
basarat

Reputation: 275927

For:

the property access is of the type of that index signature

Property access in this case is the bracket notation:

interface MapStringToFunction {
  [index: string]: Function;
}

var dict: MapStringToFunction = {};

dict['say'] = () => 'hi';

dict['say']();

More

The following will be a compile time error:

interface MapStringToFunction {
  [index: string]: Function;
  say: number; // Eroor
}

In our case, Function is the only property type that is allows:

interface MapStringToFunction {
  [index: string]: Function;
  say: Function;
}

However to be able to use any property it must be declared

Otherwise it would open up too much type-unsafe access. For example if what you propose was allowed the following would only fail at runtime and not compile time:

interface MapStringToFunction {
  [index: string]: Function;
}

var dict: MapStringToFunction = {};

dict.say() // Runtime error, as undefined is not callable

Upvotes: 3

Related Questions