Skip to content
Onsite

First<T>

EasyP2Coding

Implement First<T>, the type of the first element of a tuple T.

For an empty tuple the result is never. T must be an array type, so First<'notArray'> is a type error.

Tests your type must pass

type cases = [
  Expect<Equal<First<[3, 2, 1]>, 3>>,
  Expect<Equal<First<[() => 123, { a: string }]>, () => 123>>,
  Expect<Equal<First<[]>, never>>,
  Expect<Equal<First<[undefined]>, undefined>>,
]

// @ts-expect-error a string is not an array
type error = First<'notArray'>

Hints

TypeScript
Run and Submit type-check your solution against the tests.