-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Intl.NumberFormat
: missing options, and discussion on typing conventions
#56269
Comments
The original version of
Usually there's easily enough time between when these things get added to the spec and when the last major browser (cough Safari) implements it, so in practice it's not a huge deal.
I mean, this is always true of anything. Today Array doesn't have a
We should edit the lib when this happens. I'm not sure what else is even on the table? Regarding |
So would you prefer to keep things as-is, ie. have one typing convention for the historically defined properties and a different one for more recently added ones, to avoid any retrospective breaking? One alternative would be to keep the original library declarations as-is, but in es2023.intl (or esnext.intl), change the previously "wide" property types to the const unions defined in the standard? That would bring everything in line, but projects built against previous ES targets would be unaffected.
What method would you advocate for implementing this in the declaration files? The usual interface-merging technique isn't possible for editing existing properties, as the types for the old and new property are mismatched, and at a quick glance I can't find an obvious equivalent elsewhere in the library to go off.
This case is slightly different as it involves type changes to existing interface properties, rather than adding new ones, so has the potential to be more "breaky". Maybe I'm just overthinking. |
Nice to know I am not the only one who has noticed these problems.. |
# Pull Request ## 🤨 Rationale Fixes: #1442 ## 👩💻 Implementation The options cited in the issue are: - `useGrouping: 'auto'` - We previously were setting this to `true`, which means "display grouping separators even if the locale prefers otherwise." Instead we want `"auto"`, which is "display grouping separators based on the locale preference." The default for this option is `"auto"` unless `notation` is `"compact"`. We never set `notation` to `"compact"`, so we can just leave `useGrouping` to use the default. - `signDisplay: 'negative'` - Though this option value has been [supported in all browsers since 8/23](https://caniuse.com/mdn-javascript_builtins_intl_numberformat_numberformat_options_signdisplay_parameter_negative), there does not seem to be a version of Typescript whose definition of `Intl.NumberFormatOptions` allows it. There is an [open issue for adding support](microsoft/TypeScript#56269). As a workaround, I've just added type assertions where necessary. - `roundingPriority: 'lessPrecision'` - Just like `signDisplay: 'negative'`, the `roundingPriority` option has been [supported in browsers since 8/23](https://caniuse.com/mdn-javascript_builtins_intl_numberformat_numberformat_options_roundingpriority_parameter), but Typescript doesn't have support yet. It is tracked by the same issue linked above, and our type assertion serves as a workaround for this, too. By using this option, we can get rid of the separate `leadingZeroFormatter` in `DefaultUnitFormat` and simplify the logic. ## 🧪 Testing Existing tests pass. ## ✅ Checklist - [x] I have updated the project documentation to reflect my changes or determined no changes are needed.
@sandersn Nice.. |
#57475 maybe? |
Thank you, subscribed |
⚙ Compilation target
ESNext
⚙ Library
intl
Missing / Incorrect Definition
This was originally just going to highlight some missing NumberFormat definitions, but after a trawl through the library, rather grew in scope a bit.
I've therefore divided this issue down into three parts: two are details of changes that need to be made to the library, but the third is an RFD that arose out of inconsistencies in the Intl library definitions. If the project team feel this would be better forked elsewhere, then feel free.
1: Properties of
NumberFormatOptions
/ResolvedNumberFormatOptions
diverging from previous specificationses5.d.ts
– ECMA-402 1.0 (2012)currencySign
is defined as a property here, but does not exist in the specificationcurrencyDisplay
is not defined as a property here, but exists in the specification as"code" | "symbol" | "name" | undefined
es2020.intl.d.ts
– ECMA-402 7.0 (2020)currencyDisplay
should be removed (as should have been defined above)numberingSystem
is not defined as a property ofNumberFormatOptions
here, but exists in the specification asstring | undefined
numberingSystem
was already a property ofResolvedNumberFormatOptions
in version 1.0 of the specification; this is already correctly implemented2:
NumberFormatOptions
changes in ECMA-402 10.0 (2023)es2023.intl.d.ts
should ideally be created to house these changes.es2020.intl.d.ts
:ResolvedNumberFormatOptions
:3: Discussion – primitives versus const unions for enum-like properties
There are a lot of properties in the Intl specification that accept an enum-like list of values as valid input.
There is no set standard in the library for how these are defined.
es5.d.ts
tended to keep types as wide as possible, such asstyle?: string | undefined
compactDisplay?: "short" | "long" | undefined
The latter has obvious advantages in terms of developer usability. However, it has a significant drawback, in that these enum-like properties have had a tendency to be modified fairly frequently in recent versions of the specification.
When this occurs to properties defined using primitive types, this isn't an issue for developers, as the change is non-breaking.
For example, the
style
property was defined in the original specification as"decimal" | "percent" | "currency" | undefined
, but was added to the library asstring | undefined
; as such, when"unit"
was added as a style in ES2020, the change went unnoticed.However, when this occurs to properties defined as const unions, such as with a couple of the examples above, it's a different story.
As such, I think the project needs to decide on a couple of conventions:
"const" | "unions" | "where" | "possible"
, or should they be widened to their equivalent primitive?Sample Code
No response
Documentation Link
No response
The text was updated successfully, but these errors were encountered: