Microsoft/TypeScript
Auf GitHub ansehenIntl.NumberFormat does not model required properties when `style` is set to `currency`
Open
#57.514 geöffnet am 23. Feb. 2024
BugDomain: lib.d.tsHelp Wanted
Repository-Metriken
- Stars
- (48.455 Stars)
- PR-Merge-Metriken
- (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)
Beschreibung
⚙ 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