无法通过云功能转换ES7“包含”功能(Can not transpile ES7 “includes” feature with Cloud Functions)

编程入门 行业动态 更新时间:2024-10-05 05:20:11
无法通过云功能转换ES7“包含”功能(Can not transpile ES7 “includes” feature with Cloud Functions)

我正在尝试在VueJS项目中使用Typescript,该项目具有用于在服务器端执行Cloud Functions的子目录functions 。

我想使用ES2017功能Array.prototype.includes ,我想我通过将以下tsconfig.json添加到我的函数目录来实现这一点:

{ "compilerOptions": { "lib": ["es2016", "es2017"], "target": "es6", "module": "es2015", "moduleResolution": "node", "sourceMap": true, "strict": true } }

如果我进入函数目录并运行tsc转换器,它运行没有错误。 我的vs-code编辑器也没有强调使用“includes”的任何问题。 但是,随着Firebase文档中的建议配置,我将以下内容添加到我的firebase.json配置文件中:

"functions": { "predeploy": "npm --prefix functions run build" }

反过来,我在function目录中的package.json将构建脚本列为:

"scripts": { "build": "tslint *.ts && tsc *.ts" },

但是,当我运行firebase deploy --only functions它正确地运行构建脚本,但它然后给出以下错误:

index.ts(44,31):error TS2339:类型'string []'上不存在属性'includes'。

我相信这可能会发生,因为它错误地使用根项目目录中的tsconfig.json (也就是VueJS正在使用的那个)而不是函数目录中的那个但我不完全确定。

我可以通过将build命令更改为:

"build": "tslint *.ts && tsc *.ts --lib 'es7'"

这确实有效,但它让我觉得我已经破解了一下。 谁能指出更好的答案?

I'm trying to use Typescript in a VueJS project which has a subdirectory functions for the Cloud Functions to execute on the server side.

I'd like to use the ES2017 feature Array.prototype.includes and I thought I'd made that happen by adding the following tsconfig.json to my functions directory:

{ "compilerOptions": { "lib": ["es2016", "es2017"], "target": "es6", "module": "es2015", "moduleResolution": "node", "sourceMap": true, "strict": true } }

If I go into the functions directory and run the tsc transpiler it runs without error. Also my vs-code editor does not highlight any issue with the use of "includes". However, following along with the suggested configuration on the Firebase docs I have added the following to my firebase.json config file:

"functions": { "predeploy": "npm --prefix functions run build" }

In turn, my package.json in the functions directory has the build script listed as:

"scripts": { "build": "tslint *.ts && tsc *.ts" },

But when I run firebase deploy --only functions it correctly runs the build script but it then gives the following error:

index.ts(44,31): error TS2339: Property 'includes' does not exist on type 'string[]'.

I believe this may be happening because it is incorrectly using the tsconfig.json in the root project directory (aka, the one that VueJS is using) instead of the one in the functions directory but I'm not entirely sure.

I CAN get around this by changing the build command to:

"build": "tslint *.ts && tsc *.ts --lib 'es7'"

and this does work but it leaves me feeling I've hacked it a bit. Can anyone point to a better answer?

最满意答案

原因是当您使用tsc *.ts将输入文件指定为tsc ,将忽略tsconfig.json文件。

从文档 :

在命令行中指定输入文件时,将忽略tsconfig.json文件。

要修复此运行tsc没有任何参数。 这将使用您的tsconfig.json文件。

The reason is that when you specify input files to tsc using tsc *.ts, the tsconfig.json file is ignored.

From the documentation:

When input files are specified on the command line, tsconfig.json files are ignored.

To fix this run tsc without any parameters. This will use your tsconfig.json file.

更多推荐

本文发布于:2023-08-07 17:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   transpile   includes   Cloud   feature

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!