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
Give it a real attempt first. Run shows which examples fail.
Saved automatically
TypeScript
Run and Submit type-check your solution against the tests. ⌘Enter runs, ⌘⇧Enter submits.