Code Lab

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

Narrowing a type

FácilTypeScript types3 min

The function formatId(id: string | number) gets an order id. If it is a number, return a string with # in front, for example '#42'. If it is a string, return it in upper case.

Do not change the signature and the code at the bottom.

Ejemplos

formatId(42)

Devuelve '#42'

A number gets a hash in front.
formatId('ab-7')

Devuelve 'AB-7'

A string goes to upper case.
formatId('15')

Devuelve '15'

A string of digits is still a string.

Fuente

Función
Cargando el editor…
  1. EntradaformatId(42)
    Esperado'#42'
  2. EntradaformatId('ab-7')
    Esperado'AB-7'
  3. EntradaformatId('15')
    Esperado'15'

Las pruebas ocultas se ejecutan al enviar