从0开始创建一个 TS + React 项目(基础使用)

213 阅读1分钟

最近重拾 ts,在看文档的时候会发现有些文档会解读 ts 代码编译后的代码,想拿一个项目来专门练习 ts 和 解读 ts 相关编译原理和源码,于是就有了这个项目(只包含一些命令的基础使用项目,大佬绕道~~~)。

创建步骤

1. 全局安装 react 命令
```
npm install create-react-app -g
```
2. 创建 ts 项目,有两种方式:

方式一: 直接创建。 我没用方式一创建成功,原因不详。。。

create-react-app ts-react --scripts-version=react-scripts-ts 

方式二:分两步创建

  1. 先创建 react 项目

      ```
      // about-ts是项目名,按自己的需求命名
      create-react-app about-ts
      ```
    
  2. 安装 ts

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

image.png

3. 将项目推至 github

1. 创建一个不带 readme 的同名仓库

image.png

2. 按引导页面的命令将本地仓库和远程仓库关联上

image.png image.png

3. 启动项目
```
npm run start
```

Q&A

1. github 上的主分支默认是 main,但我的默认主分支是 master,怎么设置默认分支?

A: 可以在setting-repository 中设置 image.png

2. 新建项目之后没有引导页,怎么把本地仓库关联到远程?

A: 切换到本地代码仓库路径后,输入如下代码:

git init 
git remote add origin https://github.com/xxx/about-ts.git
git push -u origin master