Skip to content

Suggestion: use a readonly Date type in TypeScript #1944

@danielnixon

Description

@danielnixon

I've been using a ReadonlyDate type in my projects. It looks like this:

export type ReadonlyDate = Readonly<
  OmitStrict<
    Date,
    | "setTime"
    | "setMilliseconds"
    | "setUTCMilliseconds"
    | "setSeconds"
    | "setUTCSeconds"
    | "setMinutes"
    | "setUTCMinutes"
    | "setHours"
    | "setUTCHours"
    | "setDate"
    | "setUTCDate"
    | "setMonth"
    | "setUTCMonth"
    | "setFullYear"
    | "setUTCFullYear"
  >
>;

It's philosophically similar to the built in ReadonlyArray, ReadonlySet, etc.

It would be great if date-fns functions could at least accept (if not return) this type. That way an entire project could use readonly dates similarly to readonly arrays. This would give an (almost) immutability guarantee.

Returning this type would be a more drastic (and breaking) change, so probably not worth considering.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions