So say I've got this interface and object with nested properties:
interface Iobj {
a: { a2:string };
b: string;
}
const obj: Iobj = {
a:{
a2: "hello"
}
b: "world"
};
And I've got a strings that identify properties in obj:
const prop = "a.a2"
// or
const prop = "b"
I'm trying to update obj with bracket notation but these statements give me the error Type 'string' is not assignable to type 'never'.
obj[prop] = "newString";
obj[prop as keyof Iobj] = "newString";
Seems like obj[prop] isn't being recognized as valid. Something I'm doing wrong here?
obj["a.a2"]won't work even in vanilla JS; you would have to use something likelodash.get."b") works fine typescriptlang.org/play?#code/…