TypeScript – Element implicitly has an ‘any’ type because expression of type ‘string’ can’t be used to index type

You can declare colors as any to tell TypeScript to get off your back on this one (aka explicit any):

const color : any = {
    red: null,
    green: null,
    blue: null
};

But if possible, strong typing is preferable:

const color : { [key: string]: any } = {
    red: null,
    green: null,
    blue: null
};

More information on indexing in TypeScript: Index Signatures


EDIT: In this answer to a similar question, the author suggest using a Map<,> — if that fits your use-case.

Leave a Comment