Encountering
Object is possibly 'undefined'. ts(2532)
errors in TypeScript for an optional parameter, despite being clearly defined...
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' // No error occurs
Curious why a.keyValue
triggers the error while b.keyValue
does not. The difference appears to be simply in verbosity of syntax.