Last active
February 1, 2025 12:57
-
-
Save matjahs/a67a2f90f533811b64293e9e6b599cbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function isObj(arg: unknown): arg is Record<string, unknown> { | |
return Object.prototype.toString.call(arg) === '[object Object]'; | |
} | |
export function hasOwn<T = unknown>(arg: Record<string, unknown>, key: unknown): arg is Record<string, T> { | |
return ( | |
isObj(arg) && | |
Boolean(key) && | |
typeof key === 'string' && | |
key.length > 0 && | |
Object.prototype.hasOwnProperty.call(arg, key) | |
); | |
} | |
const a = { a: 1 }; | |
if (hasOwn<number>(a, 'a')) { | |
console.log(a.a); // a.a: number | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment