【发布时间】:2019-10-07 12:53:25
【问题描述】:
instanceof Date === true 似乎不满足 TypeScript 3.4.5 的基于控制流的类型分析。在下面的代码中,TypeScript 会抱怨我返回的值不是 Date,即使在我检查它确实是 Date 之后也是如此。
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,但这感觉就像我没有做正确的事情。
【问题讨论】:
标签: typescript firefox-addon-webextensions typescript3.0