如何使用Create React App和TypeScript启用可选链接 [英] How to enable optional chaining with Create React App and TypeScript

查看:376
本文介绍了如何使用Create React App和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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆