要使用 TypeScript 启动新的 Create React App 项目,您可以运行:
npx create-react-app my-app --template typescript
或
yarn create react-app my-app --template typescript
如果您之前通过
npm install -g create-react-app
全局安装了 create-react-app,我们建议您使用
npm uninstall -g create-react-app
或
yarn global remove create-react-app
卸载包,以确保 npx 始终使用最新版本。
不再支持 create-react-app 的全局安装。
要将 TypeScript 添加到现有的 Create React App 项目中,请先安装它:
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
或
yarn add typescript @types/node @types/react @types/react-dom @types/jest
接下来,将任何文件重命名为 TypeScript 文件(例如,将 src/index.js 重命名为 src/index.tsx),如果它不在项目tsconfig.json文件的根目录中,则创建 tsconfig.json。
最后重启你的开发服务器!
类型错误将显示在与 build 相同的控制台中。在继续开发或构建项目之前,您必须修复这些类型错误。有关高级配置,请参阅此处。
免责申明:本站内容大部分来自互联网,部分来源于个人原创。仅供个人学习使用,本站不承担任何法律责任。如有侵权请联系删除!