TypeScript + ES6 Map + The index signature of an object type is implicitly of type "any"

I have the following code in TypeScript:

export class Config
{
    private options = new Map<string, string>();

    constructor() {
    }

    public getOption(name: string): string {
        return this.options[name]; // <-- This line causes the error.
    }
}

And the compiler gives me this error:

Error:(10, 16) TS7017: Index signature of object type implicitly has an 'any' type.

The card is "possible" through es6-shim. I'm not quite sure what is going on here. In fact, this card confuses me a bit. It is assumed that the card should come from es6-shim, which should implement es6 functionality. But es6 has no static types, right? So why does Map expect key / value types to be used as general arguments? I saw some people add the "noImplicitAny" flag, but I want to solve the problem, not ignore it.

Thanks.

+4
source share
3 answers

ES6 Map Map.prototype.get, .

JavaScript , Map, - Map, . {} new Map() . TypeScript , , , .

+6

es6 , ? , Map , /

. , :

let foo = new Array(); // array of any 
let bar = new Array<string>(); // array of strings

foo.push(123); // okay
bar.push(123); // Error : not a string

new Array(),

.

Map , .

:

public getOption(name: string): string {
    return this.options[name] as string;
}
+2

. - ,

interface IOptions {

[propName: string]: string;

}
-1

Source: https://habr.com/ru/post/1622673/


All Articles