Code Lab

Mode entretien. Minuteur de 4 min, sans indices ni autocomplétion

First item

FacileTypeScript types4 min

Write a function first(items) that returns the first item of an array, or undefined if the array is empty. Do not change the array.

The result type must follow the array. For an array of numbers it is number | undefined, for an array of strings string | undefined. Replace unknown in the starter code. Do not change the code at the bottom, each line after @ts-expect-error must stay a type error.

Exemples

first([3, 1, 2])

Renvoie 3

The first item of an array of numbers.
first(['a', 'b'])

Renvoie 'a'

For strings the result is a string.
first([])

Renvoie undefined

An empty array has no first item.

Source

Fonction
Chargement de l'éditeur...
  1. Entréefirst([ 3, 1, 2 ])
    Attendu3
  2. Entréefirst([ 'a', 'b' ])
    Attendu'a'
  3. Entréefirst([])

Les tests cachés se lancent à la soumission