简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM