const foo = {};
['bar', 'baz'].forEach((word) => {
foo[word] = []
});
The above gives me the following error:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'. No index signature with a parameter of type 'string' was found on type '{}'.
How do I fix this? You can try it here:
CodePudding user response:
Since your object seems to have dynamic keys, you should specify it in the type of foo
.
The solution for your particular case would be
const foo : {
[key: string]: string[] // whatever type of array
} = {};
['bar', 'baz'].forEach((word) => {
foo[word] = []
});
This tells that foo
is a type of object which takes in keys of type string
and the corresponding type of value for it will be string[]