如何使用Create React App和TypeScript启用可选链接 [英] How to enable optional chaining with Create React App and TypeScript
问题描述
目前尚不支持实验语法"optionalChaining" 已启用
Support for the experimental syntax 'optionalChaining' isn't currently enabled
我遇到了以上错误.我关注了此帖子,并将"@babel/plugin-proposal-optional-chaining": "^7.7.4"
添加到了我的devDependencies
中.
I was getting the above error. I followed this post and added "@babel/plugin-proposal-optional-chaining": "^7.7.4"
into my devDependencies
.
然后我遇到了这个错误,
Then I am getting this error,
添加@ babel/plugin-proposal-optional-chaining( https://git.io/vb4Sk )到 Babel配置的插件"部分以启用转换.
Add @babel/plugin-proposal-optional-chaining (https://git.io/vb4Sk) to the 'plugins' section of your Babel config to enable transformation.
所以我遵循了这个帖子,并添加了
So I followed this post and added .babelrc
file into my project's root
{
"presets": ["react", "es2015","stage-1"],
"plugins": ["transform-runtime", "transform-optional-chaining"]
}
这似乎没有任何作用.我还听到有人提到Create React App
不允许您修改babel的配置.所以我的问题是如何在不重新接线整个整个CRA
的情况下启用可选链接?
This did not seem to do anything. I also heard someone mentioning that Create React App
does not let you modify babel's configurations. So my question is how can I enable optional chaining without re-wiring the whole CRA
?
PS .我正在使用"typescript": "^3.7.2"
,或者至少是我的package.json
所说的.我尝试了npm install
以确保已更新.不确定CRA
是否在下面做了奇怪的事情并以某种方式使用了TypeScript
的旧版本.
P.S. I am using "typescript": "^3.7.2"
, or at least that is what my package.json
says. I tried npm install
to ensure it is updated. Not sure if CRA
doing something weird underneath and using older version of TypeScript
somehow.
当我从CRA
开始项目时,我相信我们正在使用TypeScript: 3.6.x
.我想使用Optional Chaining
,所以我将package.json
文件更改为"typescript": "^3.7.2"
,然后是npm install
.我认为问题是TypeScript
知道我正在使用3.7.2
,但是CRA
仍然具有较旧的配置,并且不确定如何更新它.
When I started the project with CRA
, I believe we were using TypeScript: 3.6.x
. I wanted to use Optional Chaining
, so I changed my package.json
file to "typescript": "^3.7.2"
then npm install
. I think the problem is, TypeScript
knows that I am using 3.7.2
, but CRA
still have older configuration and I am not sure how I can update that.
推荐答案
Create-React-App使用babel来转换TypeScript,因此它没有使用npm安装的TypeScript版本. 3.3.0版本的react-scripts支持TypeScript 3.7.您可以安装它并将其与以下项一起使用:
Create-React-App uses babel to transpile the TypeScript so it isn't using your npm installed version of TypeScript. Version 3.3.0 of react-scripts supports TypeScript 3.7. You can install it and use it with:
-
yarn add react-scripts@3.3.0
-或-
npm install -s react-scripts@3.3.0
这篇关于如何使用Create React App和TypeScript启用可选链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!