简体   繁体   English

Firebase 类型和默认 tslint.json 的无隐式依赖关系

[英]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.在阅读了最新的 Firebase 博客文章为什么你应该使用 TypeScript 来编写 Cloud Functions 之后,我决定尝试tslint ,它很棒,尽管我的类型有问题。

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 告诉我以下内容。

[tslint] Module '@google-cloud/firestore' is not listed as dependency in package.json (no-implicit-dependencies) [tslint] 模块“@google-cloud/firestore”未在 package.json 中列为依赖项(无隐式依赖项)

What is the best practice with Firebase + TypeScript for using/importing types? Firebase + TypeScript 使用/导入类型的最佳实践是什么?

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 .这些出现在您的package.json文件中的functions下。 If you want to be able to import from @google-cloud/firestore , then you need to add a dependency on it:如果您希望能够从@google-cloud/firestore导入,那么您需要添加对它的依赖:

npm install @google-cloud/firestore

Now, you may be wondering why you can work with Firestore without declaring that dependency.现在,您可能想知道为什么可以在不声明依赖项的情况下使用 Firestore。 That's because the Firebase Admin SDK has its own dependency on the Firestore SDK.这是因为 Firebase Admin SDK 依赖于 Firestore SDK。 So, when you're working with the Admin SDK directly, you gain access to objects created by the Firestore SDK.因此,当您直接使用 Admin SDK 时,您可以访问 Firestore SDK 创建的对象。 But, when you don't declare the dependency yourself, your own module can't import directly from it.但是,当您不自己声明依赖项时,您自己的模块无法直接从中导入。

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 .或者,由于 Admin SDK 已经具有 firestore 依赖,您可以直接使用admin.firestore.QuerySnapshotadmin.firestore.DocumentSnapshot等,而不是安装@google-cloud/firestore

This is better approach.这是更好的方法。 You can access everything with this.您可以通过此访问所有内容。

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.它不需要将'@google-cloud/firestore'作为依赖项添加到您的项目中,并且从您的代码中消除了大量admin.firestore.xxx

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()});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM