// common.js
const boxNames = ['one', 'two'];
module.exports {
boxNames,
}
const common = require("./common.js");
const boxNames = common.boxNames;
const messages = {
'one': {
id: 'some id',
text: 'some text',
},
'two': {
id: 'some id',
text: 'some text',
},
};
const disabled = boxNames.map((key: string) => messages[key]);
The above code produces the TypeScript error for the messages[key] statement:
TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ one: { id: string; text: string; }; two: { id: string; text: string; }; }'. No index signature with a parameter of type 'string' was found on type '{ one: { id: string; text: string; }; two: { id: string; text: string; }; }'.
I am declaring key as a string and the keys of messages are strings, so what's the problem?
(Worth noting here that messages is generated using the defineMessages method from the react-intl library.)
(I have seen a couple of answers on StackOverflow similar to this that refer to Object.keys, but am not sure how this relates to my TypeScript error.)