fix(intl): require explicit options next to a fallback in the Intl formatters - #245
Merged
Conversation
…rmatters
The overloads of `number`, `datetime`, `relativetime`, `duration`, `list` and `range` accepted a lone fallback such as `number(42)`, but the runtime reads a lone argument as options, so the fallback was silently dropped, and `duration({ hours: 1 })` even threw once the object reached `Intl.DurationFormat`. A fallback cannot be told apart from options by shape, `duration` takes plain objects for both, so the contract is now spelled out in the types: a fallback always comes with options, `{}` or `false`. The custom `format` overloads of `number` and `datetime` require the function, so they no longer swallow such calls, and `relativetime` requires its options, since formatting without a `unit` throws.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #245 +/- ##
=======================================
Coverage 83.32% 83.32%
=======================================
Files 98 98
Lines 2554 2554
Branches 549 549
=======================================
Hits 2128 2128
Misses 314 314
Partials 112 112 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
The overloads of
number,datetime,relativetime,duration,listandrangeallowed a lone fallback,number(42), and the API page lists them asnumber(fallback?, options?). The runtime, however, treats the first argument as a fallback only when a second argument is present, so a lone fallback is passed to theIntlconstructor as options and silently dropped:A runtime check by shape cannot fix this consistently:
durationtakes a plain object for both the fallback and the options,listaccepts any iterable,datetimeaccepts aDate. A partial rule would cost bytes in the shared formatter and still leave some formatters needing explicit options.What
options({}for the defaults, orfalsefor raw mode).number(42)is a type error now,number(42, {})andnumber(42, false)behave as before. No runtime code changes.formatoverloads ofnumberanddatetimerequire the function and take the first two parameters as explicitlyundefined-able, so they no longer swallow such calls. The internalRangeFromtype mirrors that shape.relativetime(options?)becomesrelativetime(options): formatting without aunitthrew aTypeErrorfor any input.{}for the defaults, and the API page gets one sentence with the rule.Checks
oxlint,tsc --noEmit,vitest run(117 tests) inpackages/intlpass.