【发布时间】:2019-01-07 10:13:38
【问题描述】:
如何使用 Firebase 和我自己的 Typescript 界面?
即。我在这里有一个函数,我需要完整的Payment 对象(不仅仅是增量),所以我通过DocumentReference 对象得到它。
function doSomethingWithPayment(payment: Payment) {
console.log(payment.amount);
}
exports.resolveStripeCharge = functions.firestore.document('/payments/{paymentId}')
.onWrite((change : Change <DocumentSnapshot>) => {
change.after.ref.get().then((doc: DocumentSnapshot )=> {
const payment : Payment = doc.data(); // tslint warning here.
doSomethingWithPayment(payment);
})
[ts]
Type 'DocumentData' is not assignable to type 'Payment'.
Property 'amount' is missing in type 'DocumentData'.
const payment: Payment
我知道返回的对象不一定符合接口 - 但无论如何 - 我应该在这里做什么 - 如果我想强制输入?
我只是使用:
const payment : Payment = <Payment>doc.data();
或者有更好的解决方案吗?
【问题讨论】:
标签: typescript firebase google-cloud-firestore