I’m trying to invert an object’s fields if a value exists, but I’m getting some error that I cannot decipher.
interface Control {
name1: boolean;
name2: boolean;
...
}
const values = ['name1', 'name2', ...];
const control: Control = {
name1: false,
name2: false,
...
}
for (const value of values) {
if ((value in control) {
control[value] = !control[value]; // error
}
}
Error message:
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Control'.
No index signature with a parameter of type 'string' was found on type 'Control'.ts(7053)
However, the error goes away if I explicitly pass one of the object’s fields, such as control['name1'].
Is there something I am not understanding?
>Solution :
TypeScript doesn’t know that your values array is an array of specific strings, only that it’s a string[]. Try this:
interface Control {
name1: boolean;
name2: boolean;
}
const values = ['name1', 'name2'] as const;
const control: Control = {
name1: false,
name2: false,
}
for (const value of values) {
if (value in control) {
control[value] = !control[value]; // error
}
}
That as const tells TypeScript to specify the type of values more tightly to be ['name1', 'name2'] in this case, so when you’re iterating through it value will be typed as 'name1' | 'name2' and TypeScript will understand that those values can be used to index a Control.