属性 'toDate' 在类型 'Date' 上不存在 Angular firestore

Property 'toDate' does not exist on type 'Date' Angular firestore

我正在尝试使用 toDate().

timestamp 对象从 Firestore 转换为 TypeScript 中的 Date 对象
import { AngularFirestore } from '@angular/fire/firestore';
...
constructor(private database?: AngularFirestore) {...}
...
const someVariable = a.date.toDate();

尽管该功能在开发模式下运行良好,但我收到 TypeScript 编译检查错误。此外,由于此错误,我无法构建产品版本(使用 ng prod)。

a.date.toDate() ~~~~ src/app/project/some.service.ts:74:36 -
error TS2339: Property 'toDate' does not exist on type 'Date'.

有什么解决办法的建议吗?

错误告诉您,就 TypeScript 所知,a.date 已经 一个 Date,而不是 Timestamp。因此,无论 a 是什么,您都需要查看类型信息。如果它说它的 date 属性 是 Date 而实际上它是 Timestamp,这就是您需要修复的问题。 (或者当然,如果 a.date 已经是 Date,你不需要 toDate 调用它。但是你说这段代码是“在开发模式下工作”,这表明它是真的是Timestamp.)

toDate() 不适用于日期、字符串、数字等。将时间戳的 date 类型更改为 any

date: any;

顺便说一句,我通常会在 html 文件中使用 toDate()。它会显示错误但可以编译。输入 any 也将清除 html 错误。