Skip to content

Intl.NumberFormat does not model required properties when style is set to currency #57514

Description

@jroru

⚙ Compilation target

ESNext

⚙ Library

ESNext

Missing / Incorrect Definition

According to the MDN docs, when style is set to currency, then the currency property must be provided.

This could be achieved through a discriminating union on the style property e.g.

interface NumberFormatParamsStyleDecimal {
    style: "decimal"
}

interface NumberFormatParamsStyleCurrency {
    style: "currency"
    currency: string
}

type NumberFormatParams = NumberFormatParamsStyleDecimal | NumberFormatParamsStyleCurrency;

const numberFormat = (params: NumberFormatParams) => {};

numberFormat({
    style: "decimal",
})

numberFormat({
    style: "currency",
    currency: "USD"
})

Sample Code

This fails at runtime, but does not fail compilation:

new Intl.NumberFormat("en", {
    style: "currency",
    currency: undefined
});
VM159:1 Uncaught TypeError: Currency code is required with currency style.
    at new NumberFormat (<anonymous>)
    at <anonymous>:1:1

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#style

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do thisNeeds Human ReviewThis issue has a backlog check awaiting maintainer review.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions