问题
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. Someone knows the right dependency to achieve this?
Thank you so much
回答1:
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.
来源:https://stackoverflow.com/questions/47078285/import-firestore-dependencies-and-types-to-node-js