如果已经创建了create react app 项目,需要引入typescript
步骤1:
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
步骤2:
将项目中任何 后缀名为 ‘.js’ 的 JavaScript 文件重命名为 TypeScript 文件即后缀名为 ‘.tsx’(例如 src/index.js 重命名为 src/index.tsx )
完成前面的步骤后,如果项目正在运行中会报一个错误:
ENOENT: no such file or directory, open 'D:\zxz\learn-react\redux-education\src\index.js'
需要重新安装依赖跑项目
原来的内容会出现一些错误
如:ts配置redux时候有时候会产生以下错误
Could not find a declaration file for module 'react-redux'. 'D:/zxz/learn-react/redux-education/node_modules/react-redux/lib/index.js' implicitly has an 'any' type.
Try `npm install @types/react-redux` if it exists or add a new declaration (.d.ts) file containing `declare module 'react-redux';`
更改
const { Provider } = require('react-redux');