Code Lab

Modo entrevista. Temporizador de 4 min, sin pistas y sin autocompletado

First item

FácilTypeScript 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.

Ejemplos

first([3, 1, 2])

Devuelve 3

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

Devuelve 'a'

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

Devuelve undefined

An empty array has no first item.

Fuente

Función
Cargando el editor…
  1. Entradafirst([ 3, 1, 2 ])
    Esperado3
  2. Entradafirst([ 'a', 'b' ])
    Esperado'a'
  3. Entradafirst([])

Las pruebas ocultas se ejecutan al enviar