'Why TS raise no index signature found error in one scenario but not the other one?
I think I know the reason this error is showing but the thing I don't understand is why Typescript feels the second scenario is safe and therefore doesn't raise any error?
interface TagsType {
tag1: string;
tag2: string;
}
let tags : TagsType = {
tag1: "some tag 1",
tag2: "some tag 2",
}
Object.entries(tags).forEach(([tagName, value]) => {
/*
Element implicitly has an 'any' type because
expression of type 'string' can't be used to index type 'TagsType'.
No index signature with a parameter of type 'string' was found on type 'TagsType'
*/
tags[tagName] = value;
// Fine
tags = { ...tags, [tagName]: value };
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
