Skip to content
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

TypeScript signatures in the documentation #1607

Closed
rauschma opened this issue Jul 2, 2021 · 4 comments
Closed

TypeScript signatures in the documentation #1607

rauschma opened this issue Jul 2, 2021 · 4 comments
Labels
documentation Additions to documentation
Milestone

Comments

@rauschma
Copy link
Contributor

rauschma commented Jul 2, 2021

This seems to be the most common pattern for “like” objects:

zonedDateTime.with(zonedDateTimeLike: object | string, options?: object) : Temporal.ZonedDateTime
date.with(dateLike: object | string, options?: object) : Temporal.PlainDate
time.with(timeLike: object | string, options?: object) : Temporal.PlainTime
datetime.with(dateTimeLike: object | string, options?: object) : Temporal.PlainDateTime

Occasionally, parameter names and/or types diverge from this pattern:

yearMonth.with(yearMonthLike: object, options?: object) : Temporal.PlainYearMonth
zonedDateTime.add(duration: object, options?: object) : Temporal.ZonedDateTime
yearMonth.add(duration: Temporal.Duration | object | string, options?: object) : Temporal.PlainYearMonth
Temporal.Duration.from(thing: any) : Temporal.Duration
duration.with(durationLike: object) : Temporal.Duration

I’d prefer a more precise type because that makes the concept of “likeness” completely clear. But that may be too verbose for this documentation:

type ZonedDateTimeProps = {
  timeZone: string,
  year: number,
  month: number,
  day: number,
  hour: number,
  // Etc.
};
type ZonedDateTimeLike = Temporal.ZonedDateTime | ZonedDateTimeProps | string;

This is a nitpick, but the instance names are derived differently:

  • Temporal.ZonedDateTimezonedDateTime.year
  • Temporal.PlainDateTimedatetime.year
  • Temporal.PlainYearMonthyearMonth.year
@nikeee
Copy link

nikeee commented Jul 2, 2021

The current index.d.ts of the polyfill does this and defines them like so:

export type ZonedDateTimeLike = {
year?: number;
month?: number;
monthCode?: string;
day?: number;
hour?: number;
minute?: number;
second?: number;
millisecond?: number;
microsecond?: number;
nanosecond?: number;
offset?: string;
timeZone?: TimeZoneProtocol | string;
calendar?: CalendarProtocol | string;
};

TS offers the ability to derive types, so a Partial<T> could be used to derive a new type from T that has all fields optional (if there exists a type that could be used, we could to this instead of defining a new interface).

@ptomato
Copy link
Collaborator

ptomato commented Jul 2, 2021

Thanks for the report! To be honest, I've always considered the types in the documentation to be "TypeScript-ish", rather than correct TypeScript, in the sense that the docs use a fairly well-known syntax to indicate the types, but the text clarifies things that would otherwise be verbose to express in the type syntax. So, you're correct that it is a bit sloppy. This we probably wouldn't fix, because the documentation is migrating to MDN (see #1449) and they don't use this TypeScript-ish notation anyway.

I believe the discrepancy with strings is a bug, see #1422.

The instance names are probably a holdover from when the types were named Temporal.DateTime etc. Pull requests to fix this are welcome.

@justingrant
Copy link
Collaborator

I believe the discrepancy with strings is a bug, see #1422.

@rauschma @nikeee - We'll eventually get around to fixing things like this, but in the meantime documentation PRs are always welcome! ;-)

@ptomato ptomato added the documentation Additions to documentation label May 10, 2022
@ptomato ptomato added this to the Post Stage 4 milestone Dec 8, 2022
@ptomato
Copy link
Collaborator

ptomato commented Mar 26, 2024

Going to close this; to me it seems like a matter of taste whether the names of the this-objects are more concise or more similar to the class name, and this will probably be edited anyway if the docs ever get onto MDN.

Anyone should still feel free to reopen this and submit a PR to the docs if they feel like doing the work; it's just that I'm not intending to do it.

@ptomato ptomato closed this as completed Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Additions to documentation
Projects
None yet
Development

No branches or pull requests

4 participants