ts-data-forge
    Preparing search index...

    Variable keyIsInConst

    keyIsIn: <const K extends PropertyKey, const R extends UnknownRecord>(
        key: K,
        obj: R,
    ) => key is K & keyof typeof obj

    Type guard that checks if a key exists as an own property in an object.

    This function is similar to hasKey() but with reversed parameter order and different type narrowing behavior. While hasKey() narrows the object type, keyIsIn() narrows the key type to be a valid key of the given object.

    Type Narrowing Behavior:

    • Narrows the key type to be a key that exists in the object (K & keyof R)
    • Useful when you have a dynamic key and want to ensure it's valid for a specific object
    • The object type remains unchanged

    Implementation: Uses Object.hasOwn() to check for own properties (not inherited).

    Type Declaration

      • <const K extends PropertyKey, const R extends UnknownRecord>(
            key: K,
            obj: R,
        ): key is K & keyof typeof obj
      • Type Parameters

        • const K extends PropertyKey

          The type of the key to check, must extend PropertyKey (string | number | symbol)

        • const R extends UnknownRecord

          The type of the record (object), must extend UnknownRecord

        Parameters

        • key: K

          The key to check for

        • obj: R

          The object to check within

        Returns key is K & keyof typeof obj

        true if key is an own property of obj, false otherwise. When true, TypeScript narrows the key type to be a valid key of the object.

    const user = { id: 1, name: 'Ada' } as const;

    const maybeKey: string = 'name';

    if (keyIsIn(maybeKey, user)) {
    assert.isTrue(user[maybeKey] === 'Ada');
    } else {
    assert.fail();
    }

    hasKey - Similar function that narrows the object type instead of the key type