My function is designed to accept a date object as input and return a new date.
function makeDate(date:Date) {
return new Date(date); //<--error here
}
const newDate = new Date(); //
console.log(makeDate(newDate)); // Returns date object just fine
When working in Typescript with VS Code, I encountered the following error:
"Argument of type 'Date' is not assignable to parameter of type 'string | number'."
Although the official documentation states that the Date constructor can take a number (milliseconds) or a string (date string), I found no issue with creating a new date object by passing an existing date object into the Date constructor. This discrepancy leaves me puzzled since I anticipated no errors to occur.
I have attempted to search online for answers, but the information from Stack Overflow and GitHub did not address this specific situation or clarify the underlying problem (or perhaps I am misinterpreting their explanations in relation to my code).
Is it expected to receive this error message? Are there any possible solutions to resolve it?
Thank you!