Suggestion: use a readonly Date type in TypeScript
Author: danielnixonCreated Sep 15, 2020Updated Aug 17, 2026
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.
Source: date-fns/date-fns