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

Kim*_*Kim 4 firebase typescript tslint google-cloud-firestore

阅读最新的Firebase博客文章为什么你应该使用TypeScript编写云函数,我决定尝试tslint它,这很神奇,虽然我的类型有问题.

我有一个import语句

import { DocumentSnapshot, DocumentReference, QuerySnapshot, WriteResult, Transaction, WriteBatch } from '@google-cloud/firestore';
Run Code Online (Sandbox Code Playgroud)

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

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

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

Dou*_*son 10

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

npm install @google-cloud/firestore
Run Code Online (Sandbox Code Playgroud)

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

  • 我确实认为应该将包引用添加到peerDependencies部分(而不是依赖部分).否则,在部署您的功能时,您将收到有关重复标识符的错误(因为@ google-cloud/firestore库已经绑定到Firebase库中) (2认同)

Vai*_*tam 8

我同意接受的答案。

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

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