使用默认tslint.json的Firebase类型和无隐式依赖项

问题描述 投票:1回答:2

在阅读了最新的Firebase博客文章Why you should use TypeScript for writing Cloud Functions之后,我决定尝试使用tslint并且它很棒,尽管我的类型有问题。

我有一个import语句

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

但即使我的代码工作正常,tslint告诉我以下内容。

[tslint]模块'@ google-cloud / firestore'未在package.json中列为依赖项(no-implicit-dependencies)

Firebase + TypeScript使用/导入类型的最佳做法是什么?

typescript firebase google-cloud-firestore tslint
2个回答
8
投票

如果您希望能够从模块导入某些定义,则必须将该模块声明为依赖项。这些出现在package.json下的functions文件中。如果您希望能够从@google-cloud/firestore导入,那么您需要添加依赖项:

npm install @google-cloud/firestore

现在,您可能想知道为什么在不声明依赖性的情况下可以使用Firestore。这是因为Firebase Admin SDK对Firestore SDK有自己的依赖性。因此,当您直接使用Admin SDK时,您可以访问Firestore SDK创建的对象。但是,如果您没有自己声明依赖项,则您自己的模块无法直接从中导入。


5
投票

我同意接受的答案。

而不是安装@google-cloud/firestore。你可以使用admin.firestore.QuerySnapshotadmin.firestore.DocumentSnapshot等......

这是更好的方法。您可以使用此访问所有内容。

© www.soinside.com 2019 - 2024. All rights reserved.