在阅读了最新的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使用/导入类型的最佳做法是什么?
如果您希望能够从模块导入某些定义,则必须将该模块声明为依赖项。这些出现在package.json
下的functions
文件中。如果您希望能够从@google-cloud/firestore
导入,那么您需要添加依赖项:
npm install @google-cloud/firestore
现在,您可能想知道为什么在不声明依赖性的情况下可以使用Firestore。这是因为Firebase Admin SDK对Firestore SDK有自己的依赖性。因此,当您直接使用Admin SDK时,您可以访问Firestore SDK创建的对象。但是,如果您没有自己声明依赖项,则您自己的模块无法直接从中导入。
我同意接受的答案。
而不是安装@google-cloud/firestore
。你可以使用admin.firestore.QuerySnapshot
,admin.firestore.DocumentSnapshot
等......
这是更好的方法。您可以使用此访问所有内容。