My Partial object contains sub-properties that may be undefined and need updating.
interface Foo {
data: string
otherData: string
}
interface Bar {
foo: Foo
}
interface Baz {
bar: Bar
}
let a: Partial<Baz> = {}
//... Goal:
a.bar.foo.data = 'newData'
This action is not permitted due to potentially undefined properties. An unwieldy solution is as follows:
a = {
...a,
bar: {
...a?.bar,
foo: {
...a?.bar?.foo,
data: 'newData'
}
}
} as Partial<Baz>
Is there a more graceful way to accomplish this task?