Skip to content

NumberFormatOptions is missing several options including notation #36533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
benmccann opened this issue Jan 30, 2020 · 11 comments · Fixed by #37721
Closed

NumberFormatOptions is missing several options including notation #36533

benmccann opened this issue Jan 30, 2020 · 11 comments · Fixed by #37721
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Milestone

Comments

@benmccann
Copy link
Contributor

notation is missing from NumberFormatOptions

interface NumberFormatOptions {

TypeScript Version: 3.7.5

Search Terms: NumberFormatOptions notation

Code

const str = new Intl.NumberFormat(undefined, {notation: 'scientific'}).format(100000);

https://github.com/chartjs/Chart.js/blob/5f24b97bbfff52975c801b0a0b8880b2aa3b0a6a/src/core/core.ticks.js#L57

Expected behavior:

I should be able to use NumberFormat's notation option. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

Actual behavior:

src/core/core.ticks.js(57,43): error TS2345: Argument of type '{ notation: string; minimumFractionDigits: number; maximumFractionDigits: number; }' is not assignable to parameter of type 'NumberFormatOptions'.

Playground Link: None

Related Issues: None

@MartinJohns
Copy link
Contributor

Commonly the TypeScript team does not add experimental APIs to the standard library definitions.

You can use Declaration Merging to extend the types yourself. There's probably also a npm package out there that does this for you.

@benmccann
Copy link
Contributor Author

Thanks!

@maneetgoyal
Copy link

@MartinJohns notation is not experimental anymore. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/NumberFormat

Can this issue be reopened or I can create a new one if preffered?

@Plortinus
Copy link

@MartinJohns notation is not experimental anymore. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/NumberFormat

Can this issue be reopened or I can create a new one if preffered?

same need +1

@benmccann benmccann reopened this Mar 19, 2020
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this labels Mar 19, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 19, 2020
@NarasimhaReddyY
Copy link

@benmccann Any workaround for this for now?

@germain-receeve
Copy link

germain-receeve commented Apr 1, 2020

@benmccann Any workaround for this for now?

const shortAmount = new Intl.NumberFormat(undefined, {
  //@ts-ignore
   notation: "compact",
  compactDisplay: "short"
}).format(parseInt(value));

I think the only solution for now is to add a ts-ignore until the types definitions are updated.

@vinayakkulkarni
Copy link

I get the same error in toLocaleString() as well:

function getFormattedDistance(distance: number): string {
  return distance.toLocaleString('en-US', {
    style: 'unit',
    unit: 'kilometer',
    unitDisplay: 'narrow',
  });
}
Argument of type '{ style: string; unit: string; unitDisplay: string; }' is not assignable to parameter of type 'NumberFormatOptions'.ts(2345)

@christianjtr
Copy link

I think is related to the TS version/config, I've tested in TypeScript TS Playground and then in my current project with this config:

  • Typescript version: v4+.
  • Target: ES2020,
  • Module: ESNext.

And everything went ok.

Hope this helps.

@eugene-g13
Copy link

'trailingZeroDisplay' does not exist in type 'NumberFormatOptions'.ts(2769)

@EIQfrans
Copy link

'trailingZeroDisplay' does not exist in type 'NumberFormatOptions'.ts(2769)

https://stackoverflow.com/questions/31816061/why-am-i-getting-an-error-object-literal-may-only-specify-known-properties

const formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    trailingZeroDisplay: 'stripIfInteger'
} as Intl.NumberFormatOptions);

const price = formatter.format('500000.25'); // output: $500,000.25

@BlueManCZ
Copy link

I resolved this error by changing lib and target to ES2023 in tsconfig.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.