If I have a nested item whose type is optional, how do I set the value in a type-safe way.
For example:
type Bar = {
baz: string;
}
type Foo = {
bar: Bar
}
type PartialFoo = Partial<Foo>;
const setBaz = (p: PartialFoo, newBaz: string): PartialFoo => {
p.bar.baz = newBaz;
}
In this case, I have a type error because bar might not exist, so it's looking for a ?.. If Bar doesn't exist, I want it to exist. Is there a concise way to do this other than creating a bunch of logic testing each tier if its undefined and successively building the tree?