如何通过firebase-admin-node创建Firestore Timestamp?解决TS报错
在Firebase Node Admin SDK中创建Firestore Timestamp的正确方式
你遇到的问题是因为客户端JS SDK和Admin SDK在Timestamp的导入结构上存在差异,导致你沿用客户端的写法在Admin SDK里触发了TypeScript类型错误。
正确实现方式
在Firebase Node Admin SDK中,Timestamp是作为独立导出项存在的,你可以直接导入使用:
import { Timestamp } from 'firebase-admin'; const firestoreTimestamp = Timestamp.fromDate(new Date());
如果习惯通过firestore模块关联访问,也可以这样写:
import * as admin from 'firebase-admin'; const firestoreTimestamp = admin.firestore.Timestamp.fromDate(new Date());
不过第一种直接导入Timestamp的写法更简洁,也是官方推荐的用法。
报错原因说明
你之前的写法在客户端SDK中可行,但Admin SDK的类型定义里,从firebase-admin导入的firestore对象并没有暴露Timestamp属性——它的Timestamp是单独导出的模块成员,这和客户端SDK的挂载结构不同,所以TypeScript会提示找不到该属性。
内容的提问来源于stack exchange,提问作者DauleDK
相关产品推荐
相关产品推荐

