Typescript: The Date constructor can't be passed string | Date, but takes either individually.

Created on 26 Dec 2017  Â·  7Comments  Â·  Source: microsoft/TypeScript

TypeScript Version: 2.7.0-dev.20171209

Code

How come these are both error-free individually:

new Date(new Date())
new Date("123")

But this is a type error?

  new Date(Math.random() ? new Date() : "123");

Expected behavior:

I would expect the second code sample to not give a type error.

Actual behavior:

I get the following error:

Argument of type 'Date | "123"' is not assignable to parameter of type 'VarDate'.
  Type 'Date' is not assignable to type 'VarDate'.
    Property 'VarDate_typekey' is missing in type 'Date'.

I don't understand what VarDate is, so this is a little confusing.

Bug lib.d.ts Fixed help wanted

All 7 comments

VarDate is a type that's only supposed to be present when you use --lib scripthost 🤔

Anyway, it seems that src/lib/es5.d.ts is missing a version of the constructor that accepts string | number, and src/lib/es2015.core.d.ts is missing a version that accepts Date | string | number. (new Date appears to not officially accept Date objects before ES2015; they do work because they go through ToPrimitive, which converts them to number, which is then processed, so maybe that should be unified with es5.d.ts?)

https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.3.2

I agree VarDate should not show up normally, but that issue seems relatively old and is unrelated to this bug.

Here is a repro.

This seems to be a regression in 2.6. You can see that:

  1. It works in 2.4: https://travis-ci.org/tildeio/ts-repro-date/builds/326640424
  2. It works in 2.5: https://travis-ci.org/tildeio/ts-repro-date/builds/326641134
  3. It does not work in 2.6: https://travis-ci.org/tildeio/ts-repro-date/builds/326641480

Travis is having some issues currently so the actual tests doesn't run – but you can see that the third build fails at a much earlier step than the other two.

Yep, that is because of an intentional change to make function signatures stricter in 2.6. It's not considered a regression in the compiler; the bug is that the signatures in lib.d.ts were not updated.

thanks @alan-agius4!

@Kovensky, could you post a link to where the motivation for this stricter behavior is discussed? I can't seem to find anything about it (it doesn't seem to be a bivariance thing, and I do not have --strictFunctionTypes enabled anyway), and I'd be interested to know what the motivation was.

I've got this issue as well, just updated VS to 15.6 which prompted to update TS to 2.6 and my build broke.

Is there a workaround or should I just revert to 2.5 until this is fixed?

EDIT: It seems that if you have something like:

var date = new Date(myOtherDate);

You can temporarily convert this to:

var date = new Date(myOtherDate.getVarDate());

And it will compile.

There are two sets of overloads for Date constructor. one in the ES6/S2015 lib, this was fixed by #21757. #21757 missed the set of overloads in ES5 though. this should be fixed by https://github.com/Microsoft/TypeScript/pull/23203.

Was this page helpful?
0 / 5 - 0 ratings