Type Alias TakeFirstKeys<T, N, R>

TakeFirstKeys<T, N, R>: R["length"] extends N
    ? R
    : T extends [infer First, ...(infer Rest)]
        ? TakeFirstKeys<Rest, N, [...R, First]>
        : R

A utility type that extracts the first N elements from a tuple type T. If the tuple T has fewer than N elements, it returns the entire tuple.

This type is implemented recursively:

  • On each step, it adds the first element of the tuple T to the result array R.
  • The recursion stops when the length of the result array R reaches N.

Type Parameters

  • T extends any[]

    The tuple type from which to extract the first N elements.

  • N extends number

    The number of elements to extract from the start of the tuple.

  • R extends any[] = []

    An accumulator for storing the extracted elements (used internally in the recursion). Defaults to an empty array [].

// Extracts the first 2 elements from the tuple
type Example = TakeFirstKeys<QueryListKey<'post'>, 2>;
// Result: ['get-list', 'post']