hasOwn

Check if an obj object has a specific key property, including non-enumerable properties and symbol keys.

Usage Scenarios

  • Property Existence Check: Verify if an object contains a specific property before accessing it.
  • Type Safety: Ensure property access is safe by checking existence first.
  • Symbol Key Support: Check for symbol properties in objects.

Examples

import { hasOwn } from 'funtool';

const obj = { a: 1, b: 2 };
hasOwn(obj, 'a'); // ✅ true
hasOwn(obj, 'c'); // ❌ false

const sym = Symbol('id');
const objWithSymbol = { [sym]: 123 };
hasOwn(objWithSymbol, sym); // ✅ true

Signature

function hasOwn<T extends object>(
  obj: T,
  key: keyof T | (string & {}) | symbol | number
): boolean

Parameters

  • obj (T extends object): The object to check.
  • key (keyof T | (string & {}) | symbol | number): The key to check, can be string, symbol or number.

Returns

  • (boolean): Returns true if the object has the property, false otherwise.