Typescript 中的 React + Firebase 云函数无法部署

Wat*_*e.N 4 firebase typescript reactjs google-cloud-functions

我是我的项目,我想使用

  • 托管在 Firebase 托管上的 Typescript 中的 React 应用程序
  • 火店
  • Typescript 中的 Firebase 函数

我在部署时遇到错误。很容易重现(现在跳过 firestore 和 firebase 托管初始化)

yarn create react-app sample-app
cd sample-app
firebase init
(select functions by marking a box interactively)
(select typescript by marking a box interactively)
(uncomment in functions/src/index.ts)
Run Code Online (Sandbox Code Playgroud)

函数/src/index.ts 是

yarn create react-app sample-app
cd sample-app
firebase init
(select functions by marking a box interactively)
(select typescript by marking a box interactively)
(uncomment in functions/src/index.ts)
Run Code Online (Sandbox Code Playgroud)

然后部署失败

firebase deploy

=== Deploying to 'myproject'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /Users/username/sample-app/functions
> tslint --project tsconfig.json

Running command: npm --prefix "$RESOURCE_DIR" run build

> functions@ build /Users/username/sample-app/functions
> tsc

../node_modules/@types/testing-library__react/index.d.ts:13:49 - error TS7016: Could not find a declaration file for module '@testing-library/dom'. '/Users/username/sample-app/node_modules/@testing-library/dom/dist/index.js' implicitly has an 'any' type.
  Try `npm install @types/testing-library__dom` if it exists or add a new declaration (.d.ts) file containing `declare module '@testing-library/dom';`

13 import { queries, Queries, BoundFunction } from '@testing-library/dom';
                                                   ~~~~~~~~~~~~~~~~~~~~~~

../node_modules/@types/testing-library__react/index.d.ts:16:15 - error TS7016: Could not find a declaration file for module '@testing-library/dom'. '/Users/username/sample-app/node_modules/@testing-library/dom/dist/index.js' implicitly has an 'any' type.
  Try `npm install @types/testing-library__dom` if it exists or add a new declaration (.d.ts) file containing `declare module '@testing-library/dom';`

16 export * from '@testing-library/dom';
                 ~~~~~~~~~~~~~~~~~~~~~~


Found 2 errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/username/.npm/_logs/2020-08-04T06_14_06_195Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2
Run Code Online (Sandbox Code Playgroud)

即使我npm install @types/testing-library__dom 在 root 和 functions/ 中做了,部署也会 失败。

和根目录有

ls
README.md     firebase.json functions     node_modules  package.json  public        src           tsconfig.json yarn.lock
Run Code Online (Sandbox Code Playgroud)

欢迎任何想法。

abu*_*ick 8

你能试试这个解决方案是否适合你吗?

/sf/answers/3451659991/

将此行添加到函数文件夹中的 tsconfig :

{                                                                                       
  "compilerOptions": { 
    ...
    "typeRoots": [
      "node_modules/@types",
    ],
    ...
}
Run Code Online (Sandbox Code Playgroud)

这是对我有用的“compilerOptions”块的一部分