【发布时间】:2021-05-25 02:21:46
【问题描述】:
我目前正在尝试找到一种抽象,它可以让我运行 Firebase 产品(主要是 Firestore、Storage 和 Analytics),而不受平台(React Native、React、Node.js)的影响。我查看了 REST API,但希望将 SDK 用于它们提供的所有功能。
// web
import firebase from 'firebase';
type WebFirestore = ReturnType<typeof firebase.firestore>;
// cloud
import * as admin from 'firebase-admin';
type CloudFirestore = ReturnType<typeof admin.firestore>;
// native
import { FirebaseFirestoreTypes } from '@react-native-firebase/firestore';
type NativeFirestore = FirebaseFirestoreTypes.Module;
const API = (firestore: WebFirestore | CloudFirestore | NativeFirestore) => {
firestore
.collection('foo')
.doc('foo')
.get()
.then((resp) => true);
}
我正在尝试创建一个可以让我做同样事情的 TypeScript 类型(至少我是这么认为的)。 API 一开始就在这些产品的平台上保持一致,但我猜返回类型是不同的。我的意思是,只要 firestore 对象属于该平台上的 SDK,我就可以在所有平台上运行此函数。
我正在考虑创建一个带有标志('web'、'cloud'、'native')的类,然后在构造函数中使用 firestore 对象。我尝试运行下面的代码,但 TypeScript 显示如下:
(property) Promise<T>.then: (<TResult1 = FirebaseFirestore.DocumentSnapshot<FirebaseFirestore.DocumentData>, TResult2 = never>(onfulfilled?: (value: FirebaseFirestore.DocumentSnapshot<FirebaseFirestore.DocumentData>) => TResult1 | PromiseLike<...>, onrejected?: (reason: any) => TResult2 | PromiseLike<...>) => Promise<...>) | (<TResult1 = firebase.firestore.DocumentSnapshot<...>, TResult2 = never>(onfulfilled?: (value: firebase.firestore.DocumentSnapshot<...>) => TResult1 | PromiseLike<...>, onrejected?: (reason: any) => TResult2 | PromiseLike<...>) => Promise<...>) | (<TResult1 = FirebaseFirestoreTypes.DocumentSnapshot<...>, TResult2 = never>(onfulfilled?: (value: FirebaseFirestoreTypes.DocumentSnapshot<...>) => TResult1 | PromiseLike<...>, onrejected?: (reason: any) => TResult2 | PromiseLike<...>) => Promise<...>)
Attaches callbacks for the resolution and/or rejection of the Promise.
@param onfulfilled — The callback to execute when the Promise is resolved.
@param onrejected — The callback to execute when the Promise is rejected.
@returns — A Promise for the completion of which ever callback is executed.
This expression is not callable.
Each member of the union type '(<TResult1 = DocumentSnapshot<DocumentData>, TResult2 = never>(onfulfilled?: (value: DocumentSnapshot<DocumentData>) => TResult1 | PromiseLike<...>, onrejected?: (reason: any) => TResult2 | PromiseLike<...>) => Promise<...>) | (<TResult1 = DocumentSnapshot<...>, TResult2 = never>(onfulfilled?: (value: DocumentSnapsh...' has signatures, but none of those signatures are compatible with each other.ts(2349)
我对 TypeScript 比较陌生,想知道是否有办法使这项工作发挥作用。所有类型都单独工作,但它们的联合不起作用。有没有更好的方法来考虑 TypeScript 中的这一抽象层?我打算将它托管在 Github 包注册表和所有可以访问内部 API 的产品上,作为当前的功能 - firestore、云存储、云功能、一些 REST API 调用。
【问题讨论】:
-
TypeScript 错误显示在 .then(...)
标签: node.js typescript firebase react-native google-cloud-firestore