ts-type-forge
    Preparing search index...

    Type Alias SetAt<I, V, T>

    SetAt: SetAtImpl<T, I, V, readonly [], TSTypeForgeInternals_PinsOneNumber<I>>

    Creates a new readonly tuple type where the element at index I in T is replaced with type V.

    When I is a single numeric literal it names one position, and only that position changes. When I is a union — or number, which names no position in particular — the call replaces one of the candidate positions and never all of them, so each candidate is widened to T[I] | V rather than set to V outright.

    Type Parameters

    • I extends number

      The index to update. A single literal names one position exactly; a union of literals, or number, widens every candidate position instead. An index beyond the end of T leaves it unchanged.

    • V

      The new type for the element at index I.

    • T extends readonly unknown[]

      The readonly tuple type.

    A new readonly tuple type with the element at index I updated.

    type SA1 = Tuple.SetAt<1, 'x', [1, 2, 3]>; // readonly [1, 'x', 3]
    type SA2 = Tuple.SetAt<0 | 2, 'x', [1, 2, 3]>; // readonly [1 | 'x', 2, 3 | 'x']
    type SA3 = Tuple.SetAt<number, 'x', [1, 2, 3]>; // readonly [1 | 'x', 2 | 'x', 3 | 'x']
    type SA4 = Tuple.SetAt<2, 'x', [1, 2]>; // readonly [1, 2] (index out of bounds: unchanged)