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

3

7

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?

Resinate answered 21/1, 2018 at 10:54 Comment(0)
C
15

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.

Crepe answered 21/1, 2018 at 16:38 Comment(1)
I do believe the package reference should be added to the peerDependencies section (not the dependencies section). Otherwise when deploying your function you will get an error about duplicate identifiers (since the @google-cloud/firestore library is already caked into the Firebase library)Buote
M
14

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.

Mcmath answered 24/11, 2018 at 10:50 Comment(0)
N
0

Here's my way of doing it. It doesn't require adding '@google-cloud/firestore' as a dependency to your project, and eliminates a lot of admin.firestore.xxx from your code.

import * as admin from "firebase-admin";

import FieldValue = admin.firestore.FieldValue;
import DocumentSnapshot = admin.firestore.DocumentSnapshot;
// import anything else you want to alias

someRef.set({timestamp: FieldValue.serverTimestamp()});
Neely answered 13/9, 2020 at 2:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.