I have an object typed Partial. I want to check if a given key exists in the object. The below code doesn't work. Does anybody know how to? I'm using Typescript 4.6. Thanks in advance.
type Name = 'dog' | 'cat' | 'bird'
function(obj: Partial<Record<Name, number>>, name: Name) {
// check a given name exists in obj
if (name in obj) {
const v = obj[name] // number or undefined
}
// Wait, how about dot notation?
if ('dog' in obj) {
const v = obj.dog // number or undefined
}
if (obj.dog !== undefined) {
const v = obj.dog // number or undefined
}
}