25 lines
492 B
JavaScript
25 lines
492 B
JavaScript
|
// @ts-check
|
||
|
|
||
|
/**
|
||
|
* Get a property if it's an "own" property, otherwise return `undefined`.
|
||
|
*
|
||
|
* @example
|
||
|
* const obj = { foo: "bar" };
|
||
|
*
|
||
|
* obj.foo;
|
||
|
* // => "bar"
|
||
|
* obj.hasOwnProperty
|
||
|
* // => [Function]
|
||
|
*
|
||
|
* getOwn(obj, "foo");
|
||
|
* // => "bar"
|
||
|
* getOwn(obj, "hasOwnProperty");
|
||
|
* // => undefined
|
||
|
*
|
||
|
* @template {object} T
|
||
|
* @param {T} obj
|
||
|
* @param {keyof T} key
|
||
|
* @returns {undefined | T[keyof T]}
|
||
|
*/
|
||
|
export default (obj, key) => Object.hasOwn(obj, key) ? obj[key] : undefined;
|