react学习笔记(一)创建typescript项目

262 阅读1分钟

本文主要讲述如何让使用create-react-app创建基于typescript的react项目

安装create-react-app

$ npm i create-react-app -g

创建项目

该步骤分两种情况

1、创建全新的项目

执行如下命令:


# 生成基于typescript的react项目
$ npx create-react-app project-name --typescript

# 启动服务
$ cd project-name
$ yarn start

2、将 TypeScript 添加到 Create React App 项目

首先安装声明文件(不是全部的,具体的看项目有哪些依赖)


$ npm install --save typescript @types/node @types/react @types/react-dom @types/jest

接下来,将任何文件重命名为 TypeScript 文件(例如 src/index.js 重命名为 src/index.tsx )并 重新启动开发服务器!

详见:www.html.cn/create-reac…xue'xibi'ji学习