-
Notifications
You must be signed in to change notification settings - Fork 158
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
calendar on TimeLike TS type causes TS error in PlainTime.from
for ZDT/PDT arguments
#1707
Comments
A more DRY solution is to add |
Forgive my lack of knowledge on the Temporal types, but is it valid to pass a
Another observation: |
That's a good observation, it might be good to call TimeLike the union of "anything that can be passed where a PlainTime is expected" which is actually |
That also sounds reasonable to me too - the fewer types involved the easier Temporal will be to adopt by TS users. It would be good to try and get this right before it reaches widespread adoption given that the types are likely to end up in the typescript repo and can be hard to get changed later. |
Yes, as long as "everywhere" means all the places where a TimeLike is currently accepted.
Yep, definitely agree! For the same reason, we should probably rename these types to align with the final names of the Temporal classes, e.g.
If my fuzzy memory is correct, I believe that the original genesis of the XxxLike types was for DRY typing of both parameters to Temporal methods and for the return value of the (now removed) That said, I vaguely remember a problem with having the XxxLike types include function f(d: Temporal.DateLike = { year: 2020, month: 1, day: 1 }) {
return Temporal.PlainTime.from(d).toString();
// expected: TS error
// actual: no error, because `string` is valid for all `from` methods
} So I'm not sure that including
@12wrigja, could you share what you had in mind? |
Definitely, though I'd think this might be a breaking change if there are users using the types from the d.ts in their own code, so probably best to do this only once now rather than later when we consider this stable. I think I prefer Philip's suggestion that we try and combine together as many types as make sense and given them the name that is final / appears within the Temporal spec/docs/etc. If we then need another type to split out strings from this, I think we should be able to do something like |
It's not breaking in the TC39 sense, since TypeScript is its own thing not standardized by TC39. (All polyfills that I'm aware of are still at 0.x, so it seems OK to have a breaking change there.) |
Yep, was more thinking of "breaking" as "annoying for early adopters using TS", so esp. if we're going to change names of exported types, we should do it soon.
OK, in the short term I'll build a PR that:
Then if someone (James or anyone else, including the TypeScript team) wants to do more major surgery later, that can be a separate PR. Holler if you disagree, otherwise I'll send a PR soon for both repos. |
* Rename all XxxLike types to include the Plain prefix * Add PlainDateTime and ZonedDateTime to PlainTimeLike type (fixes tc39/proposal-temporal#1707)
* Rename all XxxLike types to include the Plain prefix * Add PlainDateTime and ZonedDateTime to PlainTimeLike type (fixes tc39#1707)
* Rename all XxxLike types to include the Plain prefix * Add PlainDateTime and ZonedDateTime to PlainTimeLike type (fixes #1707)
* Rename all XxxLike types to include the Plain prefix * Add PlainDateTime and ZonedDateTime to PlainTimeLike type (fixes tc39/proposal-temporal#1707)
The
TimeLike
TS type is the type of "PlainTime-like" plain objects that can be used in place of a PlainTime instances in Temporal methods likeZonedDateTime.p.withPlainTime
orPlainTime.from
.Currently this type looks like this:
The problem is that the
calendar
property is theCalendar
type instead ofTemporal.CalendarProtocol
. The latter is what's used for this property in all the other Temporal types'XxxLike
TS types.I understand why it was typed like this because (unlike other Temporal types) you can't give a PlainTime a userland calendar, so it will always be a
Calendar
, not aCalendarProtocol
. So when initializing a PlainTime with a TimeLike object, you want to restrict the input toTemporal.Calendar | 'iso8601'
because non-ISO calendars and userland calendars aren't allowed.The problem is that
calendar
of PlainDateTime or ZonedDateTime is a CalendarProtocol. This means that TS thinks that it's illegal to pass a PDT or ZDT instance intoPlainTime.from
, because PDT and ZDT don't satisfy the TimeLike type, because you can't implicitly cast a CalendarProtocol to a Calendar. Example:One solution may be to replace
TimeLike
withTimeLike | Temporal.ZonedDateTime | Temporal.PlainDateTime
everywhereTimeLike
is used.Or is there a better solution? @12wrigja, what do you think?
Also, #1588 is discussing whether
PlainTime.p.calendar
should be removed. If it is, this problem goes away.The text was updated successfully, but these errors were encountered: