Skip to main content

hasProperty

A function that returns a boolean indicating whether an object contains a specified property. If true, the type of the property is narrowed.


Code

🔗 View source code

Interface

typescript
const hasProperty: <T extends Record<PropertyKey, any>, K extends PropertyKey>(
obj: T,
key: K
) => key is K & keyof T;

Usage

typescript
import { hasProperty } from '@modern-kit/utils';

const exampleObj = { foo: 'foo', bar: 'bar' } as const;

const exampleKey1 = 'foo' as string;
const exampleKey2 = 'zoo' as string;

hasProperty(exampleObj, exampleKey1); // true
// const exampleKey1: "foo" | "bar"
hasProperty(exampleObj, exampleKey2); // false
// const exampleKey2: string