isKeyOf

Type guard. Determines whether an object has a property with the specified name.

Returns true if the specified property is a direct property of the object. The method returns false if the property is inherited, or has not been declared at all.

Uses Object.prototype.hasOwnProperty under the hood.

export function isKeyOf<R extends object>(
  record: R,
  key: unknown,
): key is keyof R {
  const isValidKey = typeof key === "string" || typeof key === "number" ||
    typeof key === "symbol";
  return (
    isValidKey &&
    Object.prototype.hasOwnProperty.call(record, key)
  );
}