TypeScript is throwing Object is possibly 'undefined'. ts(2532) errors for an optional parameter, even though I just defined it...
interface Foo {
keyValue?: Record<string, string>
}
const a: Foo = { keyValue: { key1: 'value' } }
a.keyValue.key2 = 'value2' // Object is possibly 'undefined'. ts(2532)
const b: Foo = {}
b.keyValue = { key1: 'value' }
b.keyValue.key2 = 'value2' // Works fine
Why does a.keyValue throw the error when b.keyValue doesn't? The only difference is slightly more verbose syntax.