TS-Scribe - v0.6.2
    Preparing search index...

    Function setIn

    Immutably sets a value at a nested path, returning a new root object.

    • Supports plain objects, arrays (including negative indices), and Maps.
    • Clones only the containers on the updated path (structural sharing).
    • Unaffected sibling branches retain their original references.
    • Missing intermediate containers are auto-created as {} or [] depending on whether the next key is numeric.
    • Every position in the path tuple is autocompleted and type-checked by the language server for known types.
    • Paths up to depth 6 have fully inferred return types. Deeper paths compile without error but return unknown.

    Note: For dynamic paths or paths that create fields not present in the original type, the return type falls back to unknown. Use type assertions when needed.

    const data = { user: { profile: { name: 'Alice' } } };
    const updated = setIn(data, ['user', 'profile', 'name'], 'Bob');
    // { user: { profile: { name: 'Bob' } } }
    // Original data.user.profile.name is still 'Alice'
    const data = { items: [{ id: 1 }, { id: 2 }] };
    const updated = setIn(data, ['items', 1, 'id'], 99);
    // { items: [{ id: 1 }, { id: 99 }] }
    const data = { tags: ['a', 'b', 'c'] };
    const updated = setIn(data, ['tags', -1], 'z');
    // { tags: ['a', 'b', 'z'] }
    const data = {};
    const updated = setIn(data, ['user', 'profile', 'name'], 'Alice');
    // { user: { profile: { name: 'Alice' } } }
    const data = {};
    const updated = setIn(data, ['items', 0, 'value'], 'first');
    // { items: [{ value: 'first' }] }
    const map = new Map([['user', { score: 100 }]]);
    const updated = setIn(map, ['user', 'score'], 200);
    // Map has {user: { score: 200 }}; original map unchanged
    const data = { matrix: [[1, 2], [3, 4]] };
    const updated = setIn(data, ['matrix', 1, 0], 99);
    // { matrix: [[1, 2], [99, 4]] }

    The root object type.

    The type of the value to set.

    The root value to update immutably.

    Tuple of string keys or numeric indices describing the path.

    The value to set at the given path.

    A new root object with the value set at the specified path. Original object is unchanged.

    • Type Parameters

      • T
      • V

      Parameters

      • object: T
      • path: []
      • value: V

      Returns V

    • Type Parameters

      • T
      • K0 extends string | number
      • V extends unknown

      Parameters

      • object: T
      • path: [K0]
      • value: V

      Returns T

    • Type Parameters

      • T
      • K0 extends string | number
      • K1 extends string | number
      • V extends unknown

      Parameters

      • object: T
      • path: [K0, K1]
      • value: V

      Returns T

    • Type Parameters

      • T
      • K0 extends string | number
      • K1 extends string | number
      • K2 extends string | number
      • V extends unknown

      Parameters

      Returns T

    • Type Parameters

      • T
      • K0 extends string | number
      • K1 extends string | number
      • K2 extends string | number
      • K3 extends string | number
      • V extends unknown

      Parameters

      Returns T

    • Type Parameters

      • T
      • K0 extends string | number
      • K1 extends string | number
      • K2 extends string | number
      • K3 extends string | number
      • K4 extends string | number
      • V extends unknown

      Parameters

      Returns T

    • Type Parameters

      • T
      • K0 extends string | number
      • K1 extends string | number
      • K2 extends string | number
      • K3 extends string | number
      • K4 extends string | number
      • K5 extends string | number
      • V extends unknown

      Parameters

      Returns T

    • Type Parameters

      • T
      • V

      Parameters

      • object: T
      • path: readonly (string | number)[]
      • value: V

      Returns unknown

    • Parameters

      • object: unknown
      • path: [
            string
            | number,
            string | number,
            string | number,
            string | number,
            string | number,
            string | number,
            string | number,
            ...(string | number)[],
        ]
      • value: unknown

      Returns unknown