Code Lab

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

Response interface

FácilTypeScript types5 min

An API returns a user with the fields id (number) and name (string). The field phone (string) is not always there. Complete the interface User and write the function contactLine(user). It returns a string like Ann: +44 20 7946 0000, and Bob: no phone for a user without a phone.

Do not change the code at the bottom. The correct calls there must compile, and each line after @ts-expect-error must stay a type error.

Ejemplos

contactLine({ id: 1, name: 'Ann', phone: '+44 20 7946 0000' })

Devuelve 'Ann: +44 20 7946 0000'

The name, a colon and the phone.
contactLine({ id: 2, name: 'Bob' })

Devuelve 'Bob: no phone'

There is no phone, so `no phone` takes its place.

Fuente

Función
Cargando el editor…
  1. EntradacontactLine({ id: 1, name: 'Ann', phone: '+44 20 7946 0000' })
    Esperado'Ann: +44 20 7946 0000'
  2. EntradacontactLine({ id: 2, name: 'Bob' })
    Esperado'Bob: no phone'

Las pruebas ocultas se ejecutan al enviar