Flutter环境下无法部署Firebase Cloud Function问题求助
Firebase云函数TypeScript部署报错问题
全新安装Flutter环境,使用TypeScript完成Firebase初始化后,部署示例云函数时出现大量TypeScript编译错误,已升级Mac上的Flutter和Firebase CLI。
错误信息
node_modules/@types/express-serve-static-core/index.d.ts:99:68 - error TS1110: Type expected. 99 type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S; ~~~ node_modules/@types/express-serve-static-core/index.d.ts:99:77 - error TS1005: '}' expected. 99 type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S; ~ node_modules/@types/express-serve-static-core/index.d.ts:99:78 - error TS1128: Declaration or statement expected. 99 type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S; ~ node_modules/@types/express-serve-static-core/index.d.ts:99:80 - error TS1005: ';' expected. 99 type RemoveTail<S extends string, Tail extends string> = S extends `${infer P}${Tail}` ? P : S; ~ node_modules/@types/express-serve-static-core/index.d.ts:101:33 - error TS1005: ';' expected. ~ Found 127 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.
我的index.ts代码
import * as functions from "firebase-functions"; // Start writing Firebase Functions // https://firebase.google.com/docs/functions/typescript export const helloWorld = functions.https.onRequest((request, response) => { functions.logger.info("Hello logs!", {structuredData: true}); response.send("Hello from Firebase!"); });
解决方案
这些错误源于当前使用的TypeScript版本不兼容${infer P}${Tail}这类模板字符串类型语法(该特性从TypeScript 4.1版本开始支持),按以下步骤修复:
- 进入functions目录,打开
package.json,将devDependencies中的typescript版本升级到4.1.0或更高,示例:"devDependencies": { "typescript": "^4.9.5" } - 删除functions目录下的
node_modules文件夹和package-lock.json文件 - 在functions目录执行
npm install重新安装依赖 - 检查
tsconfig.json的compilerOptions配置:- 将
target设为ES2018或更高版本 - 确保
lib数组包含ES2020或更高版本,示例:"compilerOptions": { "target": "ES2018", "lib": ["ES2020"], "module": "commonjs", "outDir": "lib", "rootDir": "src", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true }
- 将
- 执行
npm run build验证编译是否通过,之后再部署云函数
内容的提问来源于stack exchange,提问作者Jason Simard
相关产品推荐
相关产品推荐

