Imagine there is an interface and object with nested properties as shown below:
interface Iobj {
a: { a2:string };
b: string;
}
const obj: Iobj = {
a:{
a2: "hello"
}
b: "world"
};
Now let's say we have strings that represent the properties in obj:
const prop = "a.a2"
// or
const prop = "b"
The goal is to update obj
using bracket notation but encountering an error saying
Type 'string' is not assignable to type 'never'
.
obj[prop] = "newString";
obj[prop as keyof Iobj] = "newString";
It appears that obj[prop]
is not recognized as valid. Is there something incorrect in my approach?