TypeScript Version: 3.2.0-dev.201xxxxx
Search Terms:
date spread operator
Code
const dateFields = [2018, 5, 14, 14, 41, 11];
const date = new Date(...dateFields);
Expected behavior:
The spread syntax is used to create a new date object without a problem.
Actual behavior:
The actual date object is created, but Typescript throws the following error:
TS2556: Expected 0-7 arguments, but got 0 or more.
Playground Link:
Example Playground
Related Issues:
None
This is the common problem that the inferred type of dateFields is number[], and not a tuple as you expect it. If you explicitly type the variable to be a tuple containing 6 numbers then it works.
const dateFields: [number, number, number, number, number, number] = [2018, 5, 14, 14, 41, 11];
const date = new Date(...dateFields);
There are several issues regarding this open. It's the same problem with literal-types.
Thanks for the fast reply. I guess this kind of fixes it for the moment.
But I am using this code in an Angular project and on the first compiling when I start the project, the error still occurs and the compiling fails. If I then save a file and in this way restart the compiler, it works.
Edit: If I try to build my project, it also fails with the given error TS2556.
You should check what TypeScript version your Angular project is using. Angular limits the supported version of TypeScript you use - you can't just use the newest TypeScript version in an angular project. You need at least TypeScript version 3.0 for this feature.
Alright, thanks for the clarification! The current stable Angular release only support a TypeScript version under 2.10.
Actually, since the relevant line in DateConstructor is
new(year: number, month: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date;
(note: the first two arguments are non-nullable)
You need to define a type that can be anything between a 2 and 7 numbers array.
That goes like this:
type DateComponents = [number, number, number, number, number, number, number] | [number, number, number, number, number, number] | [number, number, number, number, number] | [number, number, number, number] | [number, number, number] | [number, number];
const dateFields: DateComponents = [year, month, <optional>];
or alternatively, you can extend the DateConstructor by this:
interface DateConstructor
{
new(...args: number[]): Date; // for new Date(...[a,b, etc]), predefined-constructor requires at least two numbers, so doesn't work with spread since that could be 0 arguments...
}
which unfortunately would allow to pass null to the first two arguments, though.
You'll need to extend the DateConstructor, to add the DateComponents variable-arguments-Constructor.
interface DateConstructor
{
new(...args: DateComponents): Date;
}
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.
Most helpful comment
This is the common problem that the inferred type of
dateFieldsisnumber[], and not a tuple as you expect it. If you explicitly type the variable to be a tuple containing 6 numbers then it works.There are several issues regarding this open. It's the same problem with literal-types.