Open lionel-rowe opened 1 year ago
As far as I can tell, this symbol is relatively new? https://stackoverflow.com/questions/73637484/namespace-intl-has-no-exported-member-localesargument
Presumably, if we add this then our .d.ts file will be incompatible with downstream ts users who don't have a relatively up to date TS. This is typically solved using typesVersions
which we don't currently have set up. Maybe we could extend the copy-types.mjs script to also set this up?
@12wrigja We've solved this for other built-in types by duplicating the types in our own .d.ts. Add long as the types match exactly, this won't break TSC in later versions of TS.
Seems simple to do the same thing here. What do you think?
Duplicating is basically what the fallback for typesVersions would do. I'm just suggesting having multiple versions so that we automatically stay up to date as the upstream Intl typings change.
And realistically wouldn't the typings that end up in the standard .d.ts for TS itself use the Intl symbols instead of a duplicate?
I'm just suggesting having multiple versions so that we automatically stay up to date as the upstream Intl typings change.
Given that TS will do declaration merging, what's the advantage of having separate per-TS-version type declaration files that would outweigh the complexity of maintaining separate files?
For example, dayPeriod
, dateStyle
, and timeStyle
are relatively new so we added them into the polyfill's types, but they work fine on later versions of TS because of declaration merging.
And realistically wouldn't the typings that end up in the standard .d.ts for TS itself use the Intl symbols instead of a duplicate?
Yep, but as long as the parts of our types match what's in TS's own lib.*
type declarations, then everything should work fine. Unless I'm missing some important point?
I've duplicated LocalesArgument
with a TODO to remove in the future.
I'd like to verify this works when compiling against TS versions that don't define LocalesArgument before merging.
Ultimately, the PR in it's current state works (as the .d.ts doesn't explicitly reference globalThis.Intl.LocalesArgument
), but I did also manage to get this to work in https://github.com/js-temporal/temporal-polyfill/pull/229 using typesVersions
and I think it has some advantages:
It leaves the index.d.ts file clean and enables automatic forward compatibility with any changes to the upstream definition of Intl.LocalesArgument (the real definition of which is decided by our users, not whatever version of TS we compile against).
Thoughts @justingrant?
@lionel-rowe I'd be more than happy for you to copy my WIP PR above and fix it up if you'd still like to contribute this fix.
Thanks @12wrigja — I've updated PR with that fix. I've set the threshold at <4.7.4, as 4.7.4 has the type but 4.6.4 is missing it.
I'm happy with this, but would like @justingrant to take a look too.
After reading over https://twitter.com/atcb/status/1634653474041503744?s=46&t=QSggAfTZ89VDmJRcZoeQ0A, I think we might need some small adjustments to the build so that this works consistently (as typesVersions is only used when exports is not).
https://github.com/js-temporal/temporal-polyfill/blob/main/copy-types.mjs needs updating to copy the new .d.ts file to a .d.cts file
Based on https://github.com/microsoft/TypeScript/pull/50890#issuecomment-1256528709.
Just ran into this, as I encountered this typing issue myself when trying to pass some Intl.LocalesArgument
typed variable to the toLocaleString
function. Is there any chance of this still getting merged at some point?
This PR allows
locales
parameters to acceptIntl.Locale
objects (or arrays thereof). This already works fine in plain JS, but the current TS types are overly restrictive.See also: https://github.com/microsoft/TypeScript/issues/52946
Note that
Intl.DateTimeFormat
already acceptsIntl.Locale
s — the casting toas ConstructorParameters<typeof IntlDateTimeFormat>[0]
is only needed until the TSlib
issue is fixed.