Narrows an unknown key to a known key of the object.
Object whose own properties to check.
Unknown key to test.
true when key is an own property of object, narrowing it to keyof T; false for inherited or absent keys.
true
key
object
keyof T
false
const obj = { foo: 1, bar: "hello" };declare const key: string;if (hasKey(obj, key)) { obj[key]; // ^? number | string} Copy
const obj = { foo: 1, bar: "hello" };declare const key: string;if (hasKey(obj, key)) { obj[key]; // ^? number | string}
Narrows an unknown key to a known key of the object.