我在将TypeScript添加到已经存在的 create-react-app应用程序时遇到困难。以前,我总是在启动项目之前就添加了它,只需通过即可create-react-app my-app --scripts-version=react-scripts-ts
,但是现在不起作用。
我在这里找到的唯一“解决方案” 是:
react-scripts-ts
tsconfig.json
,tsconfig.test.json
,tslint.json
从临时项目src文件夹到项目的src
文件夹中。package.json
,将脚本部分中的所有引用更改react-scripts
为react-scripts-ts
。这似乎是一个怪异的解决方法,并且效率不高。有谁知道是否可以用比这更好的方式添加它?
从react-scripts 2.10开始,您可以将TypeScript添加到现有项目中或在创建新项目时。
现有项目
yarn add typescript @types/node @types/react @types/react-dom @types/jest
...然后将.js
文件重命名为.tsx
新项目
yarn create react-app my-app --typescript
您可以在这里阅读更多内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句