Skip to content

Instantly share code, notes, and snippets.

@matjahs
Last active February 1, 2025 12:57
Show Gist options
  • Save matjahs/a67a2f90f533811b64293e9e6b599cbf to your computer and use it in GitHub Desktop.
Save matjahs/a67a2f90f533811b64293e9e6b599cbf to your computer and use it in GitHub Desktop.
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