Code Lab

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

Narrowing a type

FacileTypeScript 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.

Exemples

formatId(42)

Renvoie '#42'

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

Renvoie 'AB-7'

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

Renvoie '15'

A string of digits is still a string.

Source

Fonction
Chargement de l'éditeur...
  1. EntréeformatId(42)
    Attendu'#42'
  2. EntréeformatId('ab-7')
    Attendu'AB-7'
  3. EntréeformatId('15')
    Attendu'15'

Les tests cachés se lancent à la soumission