1

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)?

Croad Langshan
  • 2,646
  • 3
  • 24
  • 37
  • You can't. There's only the Date type, whether you care about the date part, the time part, or both. You could use _branded types_ to keep those separate within your code, though. – jonrsharpe Jan 03 '22 at 11:18
  • DO you mean something like [this](https://catchts.com/dates) ? – captain-yossarian from Ukraine Jan 03 '22 at 11:20
  • @jonrsharpe by "ideally without implementing it myself", I don't mean that I want to use `Date` without adding any additional code. I mean that ideally I'd like to use a standard solution if one exists. – Croad Langshan Jan 03 '22 at 11:23
  • 1
    Then see e.g. https://stackoverflow.com/q/26810574/3001761. – jonrsharpe Jan 03 '22 at 11:25
  • @captain-yossarian Without reading the whole thing, it looks like they want a date-only time, yes -- but they also try to do that using template literals. So that sounds like it may be a valid answer, but I think a template literal solution does more than I need and adds quite a bit of complexity: I'm fine with `Date` (or an object) as the runtime representation, I just want a type that cannot take date+time values. – Croad Langshan Jan 03 '22 at 11:30
  • @CroadLangshan could you please add some examples of what you want to achieve with expected errors and valid representation? It is a bit vogue for me – captain-yossarian from Ukraine Jan 03 '22 at 11:32
  • @jonrsharpe Thanks, that looks like one possible generalisation of this question. I intend this question to be specifically about dates, though. Also, I can imagine other solutions to this question that don't use that language feature (for example, using a wrapper class). – Croad Langshan Jan 03 '22 at 11:40
  • @captain-yossarian I've added some examples. I don't think I'm wedded to any particular representation: as I say, I think your suggestion is probably a valid answer, just higher-powered than is necessary given the question. – Croad Langshan Jan 03 '22 at 11:58
  • Do you expect `OnlyDate` to be a template literal string ? Which code example (from [this answer](https://stackoverflow.com/questions/66563064/how-do-i-typescript-this-object-array#answer-66564065)) is valid for your use case ? – captain-yossarian from Ukraine Jan 03 '22 at 12:16
  • @captain-yossarian I don't expect any use of template literals to be needed. I don't quickly see any part of that answer that's very relevant, because of that. – Croad Langshan Jan 03 '22 at 12:24
  • 1
    The objective is to provide general answers you can apply to your specific problem. If you find those existing solutions (or using a wrapper class, if you've actually tried that) don't meet your needs, _then_ you can ask a more specific question that explains how they failed and what additional constraints you have. – jonrsharpe Jan 03 '22 at 13:00
  • I appreciate your hard work answering questions and moderating. I asked this question because I think questions like this one -- fairly specific but of potential general interest -- are perhaps the most useful thing about SO. Really not making this up: I'd implemented something that works for me before I looked back to see the first comment (which I may post as an answer later), but I thought, and still do, that this question would help others, and would also let me see how other people tackle this better than me. – Croad Langshan Jan 03 '22 at 15:21

1 Answers1

1

I think the Google term you didn't quite hit on is "calendar date type". There are a couple of libraries that provide this type for Typescript. The one that looks like it's under more active development (and which doesn't make a strange choice--more on that below) is calendar-date.

The other option is typescript-calendar-date, but that represents months as a three-character string, which I find unfortunate.

rswerve
  • 169
  • 1
  • 3
  • 7