修复"firebase-admin"和"firebase"软件包之间的类型不兼容 [英] Fixing type incompatibility between `firebase-admin` and `firebase` packages

查看:51
本文介绍了修复"firebase-admin"和"firebase"软件包之间的类型不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写旨在在客户端代码或云功能内操纵Firestore数据的功能.

I am writing functions that are designed to manipulate Firestore data either within client code or within cloud functions.

不幸的是,似乎 firebase (用于客户端代码)和 firebase-admin (用于云函数)的TypeScript类型虽然几乎相同,但却不兼容.除了导入和别名所有类型外,还有人有解决方案吗?

Unfortunately, it seems like the TypeScript types for firebase (used for client code) and firebase-admin (used for cloud functions), while almost identical, are incompatible. Does anyone have a solution besides importing and aliasing all the types?

当前,我遇到类似以下错误:

Currently, I'm getting errors like:

类型'FirebaseFirestore.DocumentReference'不能分配给类型'firebase.firestore.DocumentReference'

这是因为我正在使用 firebase 在客户端代码库中定义接口,然后尝试通过 firebase-admin 初始化实例并在函数代码库中使用它.但是,如果我交换它们,将会发生相同的错误.

This is because I am defining my interfaces in my client codebase using firebase, but then trying to initialize an instance and use it in my functions codebase using firebase-admin. But the same error would occur if I swapped them.

不幸的是,即使从接口的角度来看,Node Admin SDK TypeScript定义似乎也不是Client TypeScript定义的超集.

Unfortunately, it looks like the Node Admin SDK TypeScript definitions are not a superset of the Client TypeScript definitions, even though they appear to be do from an interface perspective.

推荐答案

您可以创建自己的接口,该接口复制客户端和服务器SDK的公共元素,并围绕它们的实现实现包装,以根据需要进行切换.包装器将简单地委派给预期的目的地,并可能还会根据需要转换任何哨兵值,例如FieldValue.serverTimestamp()返回的值.

You can create your own interface that copies the common elements of the client and server SDKs, and implement a wrapper around their implementations to switch as needed. The wrapper will simply delegate to the expected destination, and also perhaps translate any sentinel values such as those returned by FieldValue.serverTimestamp() as needed.

换句话说,没有简单的解决方案.就像在多个实现上创建任何抽象一样,只是API通常不需要任何翻译.

In other words, there is no easy solution. This is just like creating any abstraction over multiple implementations, except the APIs generally don't need any translation.

这篇关于修复"firebase-admin"和"firebase"软件包之间的类型不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆