DEV Community

Discussion on: TypeScript is wasting my time

Collapse
 
gmartigny profile image
Guillaume Martigny

As stated 2 times, I know this is mostly due to a lack of knowledge. So thanks a lot for the encouragements. I'm fully aware that a project with 80K stars and 33K commits over 650 contributors is not a failure.

Also, thanks for the two advices. BTW, @ryands17 taught me that Intl.NumberFormatOptions is an existing type.

Collapse
 
lioness100 profile image
Lioness100 • Edited

An easier way to do this is

const options = {
  ...
} as const;
Enter fullscreen mode Exit fullscreen mode

The "const assertion" will concrete the value to "compact" instead of string

Edit: oops, someone already mentioned that, sorry.

Collapse
 
fellz profile image
Roman

You should specify that notation not just the string type but concrete type Intl.NumberFormatOptions['notation']

  function formatNumber(value: number, lang: string = 'en') {
  const options = {
    notation: 'compact' as Intl.NumberFormatOptions['notation'],
    maximumFractionDigits: 1,
  };
   const formatter = Intl.NumberFormat(lang, options);
   return formatter.format(value);
}
Enter fullscreen mode Exit fullscreen mode