Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase types and no-implicit-dependencies with default tslint.json

After reading the newest Firebase blog post Why you should use TypeScript for writing Cloud Functions, I decided to try tslint and it's amazing, though I have a problem with my types.

I have an import statement as this

import { DocumentSnapshot, DocumentReference, QuerySnapshot, WriteResult, Transaction, WriteBatch } from '@google-cloud/firestore';

But even though my code is working fine tslint tells me the following.

[tslint] Module '@google-cloud/firestore' is not listed as dependency in package.json (no-implicit-dependencies)

What is the best practice with Firebase + TypeScript for using/importing types?

like image 284
Kim Avatar asked Jan 21 '18 10:01

Kim


2 Answers

If you want to be able to import some definitions from a module, you have to declare that module as a dependency. These appear in your package.json file under functions. If you want to be able to import from @google-cloud/firestore, then you need to add a dependency on it:

npm install @google-cloud/firestore

Now, you may be wondering why you can work with Firestore without declaring that dependency. That's because the Firebase Admin SDK has its own dependency on the Firestore SDK. So, when you're working with the Admin SDK directly, you gain access to objects created by the Firestore SDK. But, when you don't declare the dependency yourself, your own module can't import directly from it.

like image 65
Doug Stevenson Avatar answered Oct 19 '22 10:10

Doug Stevenson


I agree with accepted answer.

Alternatively, since Admin SDK already has firestore dependancy, You can directly use admin.firestore.QuerySnapshot, admin.firestore.DocumentSnapshot etc. instead of installing @google-cloud/firestore.

This is better approach. You can access everything with this.

like image 21
Vaibhav Kumar Gautam Avatar answered Oct 19 '22 10:10

Vaibhav Kumar Gautam