The control flow based type analysis in TypeScript 3.4.5 does not seem to be satisfied by instanceof Date === true
. Even after confirming that the value is a Date, TypeScript complains that the returned value is not a Date.
async function testFunction(): Promise<Date> {
const {testDate}: {testDate: Date | string} = await browser.storage.local.get({testDate: new Date()});
if (testDate instanceof Date === true) {
// Despite checking for Date type, TypeScript still complains about Type 'string | Date' not being assignable to type 'Date'.
// Changing the line to <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ea9e939a8f998998839a9eaad9c4dec4df">[email protected]</a> as Date may solve the issue but doesn't feel right.
return testDate;
} else if (typeof testDate === "string") {
return new Date(testDate);
}
}
Using return testDate as Date
might work, but it seems like there could be a better solution.