react项目添加ts

新项目创建

1、react + antd + ts

yarn create react-app xxx --template typescript

安装antd

yarn add antd

2、react + ts

yarn create-react-app xxx --template typescript

已有react项目

1、安装ts

npm i typescript -g 全局安装
npm i typescript -D 当前项目安装

2、tsc --init

修改tsconfig配置文件

{
     
  "compilerOptions": {
     
     "target": "es5", /**指定ECMAScript目标版本**/                   
     "module": "commonjs", /**指定生成哪个模块系统代码**/ 
     "allowJs": true,  /**允许编译js文件**/                     
     "jsx": "preserve",  /**支持JSX**/                  
     "outDir": "build",  /**编译输出目录**/    
     "strict": true, /**启用所有严格类型检查选项**/ 
     "noImplicitAny": false, /**在表达式和声明上有隐含的any类型时报错**/          
     "skipLibCheck": true,  /**忽略所有的声明文件的类型检查**/                   
     "forceConsistentCasingInFileNames": true   /**禁止对同一个文件的不一致的引用**/   
  },
  "include": ["src"] /**指定编译目录**/ 
}

3、安装依赖(npm —S)

yarn add typescript
yarn add @types/react @types/react-dom

你可能感兴趣的:(React,TypeScript,前端开发,react,typescript)