I am using Firebase's Admin SDK. It also offers to use the Firebase Database as admin.
The type definition for the created Firebase Admin App looks as follows:
import * as firebaseAdmin from 'firebase-admin';
export type FirebaseContext = {
firebaseAdmin: firebaseAdmin.app.App;
};
So far, it worked fine. However, if I am using Firebase's TIMESTAMP
feature, it throws a type error:
firebaseAdmin.database.ServerValue.TIMESTAMP
// Property 'ServerValue' does not exist on type '(url?: string | undefined) => Database'.ts(2339)
It should be said that the firebaseAdmin
instance comes as argument from a function and is thus defined as FirebaseContext.firebaseAdmin
from earlier.
So, did I do anything wrong with the earlier type definition? Because, if I don't pass the firebase instance via the function's arguments but import it directly from its module, it doesn't complain about the type definition. So there must be something wrong with the type FirebaseContext
.
Thanks for your time and help.
As far as I can see you are now trying to get the database.ServerValue.TIMESTAMP
object from a FirebaseApp
instance. But the object is actually defined on the global firebase
, and not on an app instance. So you will have to explicitly import firebase
/admin
and call database.ServerValue.TIMESTAMP
on that, i.e. firebase.database.ServerValue.TIMESTAMP
or admin.database.ServerValue.TIMESTAMP
.
Yes. That’s what I did in the end. I was just wondering if I could do something about the type to make it right for passing the Firebase instance as argument in functions.
It's not about type coercion, but about the actual object that you use. A
FirebaseApp
instance has adatabase()
method, but not thedatabase
property thatServerValue.TIMESTAMP
is defined on.Now I get it. On the Firebase instance, the database is a function, on the imported object, it isn’t. Thanks! :)
Good to hear that it clicked Robin. I sometimes wonder if it was worth separating the (global) values from the app instance values. The approach we used saves memory, but you're definitely not the first one to fall into this trap. :-/