Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing type incompatibility between `firebase-admin` and `firebase` packages

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

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:

Type 'FirebaseFirestore.DocumentReference' is not assignable to type 'firebase.firestore.DocumentReference'

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.

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.

like image 400
Trevor Lohrbeer Avatar asked Apr 08 '19 17:04

Trevor Lohrbeer


1 Answers

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.

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.

like image 184
Doug Stevenson Avatar answered Nov 20 '22 11:11

Doug Stevenson