-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inaccurate type for return value of Intl.RelativeTimeFormat.prototype.formatToParts
#46245
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
Comments
Cool, happy to have I'm not too wild on making the functions generic to support some of the narrowing, we really want to keep the shipped libs as simple as possible because they get included in everyone's projects and historically a lot of the times we've been clever we've had to backtrack |
OK, I'll send a PR soon. I also noticed another Intl type-declaration issue: the input properties to the But the Here's the types for reference: Lines 4392 to 4406 in 5d0d7ae
Lines 4408 to 4423 in 5d0d7ae
OK, sounds reasonable, I won't include (2) in the PR. What about (3) above which is a discriminated union not a generic? |
FWIW, newer updates to Intl are inconsistent with older libs' types. Older libs (excerpted in my earlier comment) use TypeScript/src/lib/es2021.intl.d.ts Lines 3 to 18 in 61d2939
Any idea why they are different? Should they be different? |
Generally we want to use a union of string literals when it makes sense which is most of the time, but it's can be hard to do that (which means falling back to As seen in something like TypeScript/src/lib/es2018.intl.d.ts Lines 41 to 44 in 61d2939
Which is both correct... and incorrect. The other is that these are contributions from a range of people, and while I gave them all big look over for 4.5, that doesn't make them all perfectly consistent and accurate (because I've barely used these APIs in production) - so we're OK to back breaking changes like |
Makes sense. Would you welcome a PR to change
Could you explain the problem a little more? Not sure I fully understand it. Thanks! |
This commit updates the type of `RelativeTimeFormatPart` to clarify that the `unit` prop is always singular, unlike the plural or singular values that are accepted as inputs. This also changes `RelativeTimeFormatPart` to be a discriminated union type because the `unit` prop is only present if the `type` prop's value is not "literal". Fixes microsoft#46245
This commit updates the type of `RelativeTimeFormatPart` to clarify that the `unit` prop is always singular, unlike the plural or singular values that are accepted as inputs. This also changes `RelativeTimeFormatPart` to be a discriminated union type because the `unit` prop is only present if the `type` prop's value is not "literal". Fixes microsoft#46245
This commit updates the type of `RelativeTimeFormatPart` to clarify that the `unit` prop is always singular, unlike the plural or singular values that are accepted as inputs. This also changes `RelativeTimeFormatPart` to be a discriminated union type because the `unit` prop is only present if the `type` prop's value is not "literal". Fixes #46245
Bug Report
The types for
Intl.RelativeTimeFormat.prototype.formatToParts
in lib.e2020.intl.d.ts have a few issues.According to the spec (and MDN docs), either singular or plural names for units are accepted as input, but only singular names are output. The current types assume that plural names can also be output, which is incorrect. The relevant step in the spec is Step 5 in https://tc39.es/ecma402/#sec-PartitionRelativeTimePattern, where plural inputs are converted to singular before being used in the output.
Also, there's a missed opportunity here to narrow the return type of this method, because for any input unit, the return unit will be the same unit, unless it's a plural in which case it's the plural unit converted to singular. (See https://tc39.es/ecma402/#sec-makepartslist.) So the return type could be a generic where
unit
in the output depends onunit
in the input.Finally, there's another missed opportunity to narrow the return type using the
type
property, because according to https://tc39.es/ecma402/#sec-FormatRelativeTimeToParts and https://tc39.es/ecma402/#sec-makepartslist, iftype === 'literal'
thenunit
will be omitted, while iftype
is any other value, thenunit
will be present in the output.I'd be happy to submit a PR to change these types. (If a PR would be welcome.)
Here's excerpts from the current relevant types in lib.e2020.intl.d.ts:
TypeScript/lib/lib.es2020.intl.d.ts
Lines 114 to 118 in cb158e7
TypeScript/lib/lib.es2020.intl.d.ts
Lines 183 to 186 in cb158e7
TypeScript/lib/lib.es2020.intl.d.ts
Lines 38 to 47 in cb158e7
🔎 Search Terms
Intl
RelativeTimeFormat
formatToParts
plural
singular
🕗 Version & Regression Information
Introduced in #36084.
⏯ Playground Link
https://www.typescriptlang.org/play?target=99#code/MYewdgzgLgBATlAZjAvDMBTA7jAkmKAGwDoAlDQgQygEsA3DAFRoFsMAxEOF6gCgEoA3AFgAUKEiwADpQQRU8JMURceURiAAKsqBF4AWAEwAaGAHIWNMAFcoGCGaFiA9M5jkI1wlACqYGrA08lAAFhgwlMBQ1pSEMFAAnlLhcBjRcJgAJjDJcPFhMACiAMIAsgCCALT6AAyGMBDJwGKJye72Xr7+sGgARAkYsr0wAD4wvQCOMQgYcMNjvSzgofPjWBgYANarvZmUCcOj4yEg1nNHi1a2GDsQGBKZvSLi4NA5OhAA6gEhfgEKMjkyho3lmvCkqAAfDliNZuk4XpIYHCAgAudqebx-HrvOTfULYgDaNQAurDugBCZ6uGC0mAAPUZ9JcbkYSXCZnIVFoDGYbE43Go2LMMCC6BAsEoEAgNAA5mBKAAjQjhKAgeLs8weTrC4gsmBstpmfqDOAQXoisVgCURaVyhXK1Xq1oc7VY7pmYi8QwAZkMhn4YiAA
💻 Code
🙁 Actual behavior
TS error 2322
🙂 Expected behavior
No error because the unit of the return type is always singular.
The text was updated successfully, but these errors were encountered: