I'd like to use the TypeScript type system to distinguish variables that represent only dates (no time component) from variables with a type like Date
that can represent dates with a time ("date-time" values).
I'm aware of course that Date
can represent dates, but without a distinction in the type system I think I'm more likely to get my intentions about dates and date-times muddled up.
An example of a system that makes this distinction is the Python datetime library, which has both date
and datetime
types https://docs.python.org/3/library/datetime.html
For example (but the API need not be exactly like this):
const date: OnlyDate = makeOnlyDate()
const datetime: Date = new Date()
date + datetime // type error: perhaps Operator '+' cannot be applied to types 'OnlyDate' and 'Date'
let datetime2: Date = date // type error: perhaps Type 'OnlyDate' is not assignable to type 'Date'
let date2: OnlyDate = datetime // type error: perhaps Type 'Date' is not assignable to type 'OnlyDate'
date.getSeconds // type error: Property 'getSeconds' does not exist on type ...
How can I do that? Ideally without implementing it myself (i.e. a standard solution would be ideal)?