Code Lab

Narrowing a type

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

Examples

formatId(42)

Returns '#42'

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

Returns 'AB-7'

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

Returns '15'

A string of digits is still a string.

Source

Function
Loading the editor...
  1. InputformatId(42)
    Expected'#42'
  2. InputformatId('ab-7')
    Expected'AB-7'
  3. InputformatId('15')
    Expected'15'

Hidden tests run on Submit.