Francisco Durdin Garcia
Francisco Durdin Garcia

Reputation: 13327

Import Firestore dependencies and types to node.js

I'm recently updating my cloud functions to TypeScript after the talks on the FirebaseSummit of this year.

All my code looks quite cool, but I'm having some problems trying to recover the types of Firestore API, such QuerySnapshot , DocumentReference...

async function getEventsMadeByUser(userId: string): Promise<FirebaseFirestore.DocumentSnapshot[]> {
    const eventsMadeByUserQuery = await instance.collection('PrivateUserData')
        .doc(userId).collection(EVENTS)
        .where('interactionUser', '==', userId).get();
    return eventsMadeByUserQuery.docs;
}

Right now my IDE (WebStorm) is not getting the types for FirebaseFirestore. This is how my package.json looks:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "dependencies": {
    "firebase-admin": "^5.4.3",
    "firebase-functions": "^0.7.2"
  },
  "devDependencies": {
    "@types/express": "^4.0.37",
    "typescript": "^2.3.2"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc --watch",
    "deploy": "tsc && firebase deploy --only functions"
  },
  "main": "build/index.js",
  "private": true
}

I have already tried to add @firebase-firestore and nothing, it's not working. What is the right dependency to achieve this?

Upvotes: 3

Views: 3160

Answers (3)

Marcel Otten
Marcel Otten

Reputation: 250

I figured out that you can get the types directly from firebase-admin without adding additional packages:

import { firestore } from 'firebase-admin'

const iAmConsumingDB = (db: firestore.Firestore) => {
  // do something
}

const iAmConsumingADocumentReference = (reference: firestore.DocumentReference) => {
  // do something
}

Upvotes: 1

hon2a
hon2a

Reputation: 7214

import { DocumentData } from '@firebase/firestore-types'

Upvotes: 2

SUPERCILEX
SUPERCILEX

Reputation: 4007

I finally figured it out! The firebase-admin module is just a simple wrapper that initializes Firestore for you. To get the real Firestore, add "@google-cloud/firestore": "^0.9.0" to your package.json. You can admire those beautiful types here: https://github.com/googleapis/nodejs-firestore/blob/master/types/firestore.d.ts.

Upvotes: 9

Related Questions