instanceof Date === true
, по-видимому, не удовлетворяет анализу типа потока управления в TypeScript 3.4.5. В приведенном ниже коде TypeScript будет жаловаться, что возвращаемое мной значение не является датой, даже после того, как я проверил, что это действительно дата.
async function testFunction(): Promise<Date> {
const {testDate}: {testDate: Date | string} = await browser.storage.local.get({testDate: new Date()});
if (testDate instanceof Date === true) {
// typescript@3.4.5 will complain:
// Type 'string | Date' is not assignable to type 'Date'.
// Type 'string' is not assignable to type 'Date'.
return testDate;
} else if (typeof testDate === "string") {
return new Date(testDate);
}
}
Я мог бы сменить проблемную линию на return testDate as Date
, но такое чувство, будто я делаю не то, что нужно.