【发布时间】:2020-08-31 05:17:48
【问题描述】:
我有一个界面:
export interface User {
uid: string;
createdAt: Timestamp | Date;
}
在我的一个组件模板中,我读取了属性:
<span>{{ user.createdAt._seconds }}</span>
但是在我的终端中,我得到了下一个错误
“日期 | 类型”上不存在属性“_seconds”时间戳'。 “日期”类型不存在属性“_seconds”。
它存在于类型Timestamp 中,但看起来typescript 并不关心它并抛出错误,因为该属性在类型Date 中不存在。
【问题讨论】:
-
你能 console.log() 组件 ts 文件中填充的用户对象吗?它看起来像什么?
-
它存在于
Timestamp类型中,但它不存在于Date类型中,并且TS无法判断在这种特殊情况下createdAt是否包含Timestamp或Date.因此,执行代码时该属性不存在的可能性为 50/50。
标签: angular typescript typescript-typings