Type Alias At<A, N, Fail>

At<A, N, Fail>: IndexAt<A, N, Fail, Fail, Fail> extends infer I
    ? I extends number
        ? IsTuple.$<A, {
            $else: A[I] | undefined;
            $then: NumberPlus.IsNumber.$<I, {
                $else: A[I];
                $then: A[I] | undefined;
                exact: true;
            }>;
        }>
        : Fail
    : never

🦴 utilities

Gets the type of the array or tuple at positive or negative index N.

Like Array.at(), this type supports negative numbers.

Type Parameters

  • A extends readonly unknown[]
  • N extends number
  • Fail = never

https://github.com/microsoft/TypeScript/issues/53345#issuecomment-1477138167

type R = At<[1, 2, 3], 2> // 3
type R = At<[1, 2, 3], -1> // 3