编译器的使用

安装typescript编译器tsc

  1. yarn global add typescript

编译 typescript 文件

  1. tsc test.ts

这将生成同名的js文件

编译目录下的所有ts文件:

  1. tsc -p .

在VSCode中运行ts文件

  1. 在 VSCode 中安装插件 Code Runner

001.png

  1. 安装 ts-node

    1. yarn global add ts-node
  2. 右键运行

002.png

在VSCode实时编译输出js文件

  1. 全局安装typescript

    1. yarn global add typescript
  2. 使用 tsc init 创建一个 tsconfig.json 文件,内容大致如下:

    1. {
    2. "compilerOptions": {
    3. "target": "es5",
    4. "module": "commonjs",
    5. "outDir": "./js",
    6. "strict": true,
    7. "esModuleInterop": true,
    8. "skipLibCheck": true,
    9. "forceConsistentCasingInFileNames": true
    10. }
    11. }
  3. 输入命令

    1. tsc -w -p .

    或者在VSCode菜单中找到:
    终端 > 运行任务 > 全部任务 > tsc: 监视
    Snipaste_2021-02-20_16-20-07.webp

然后就可以左右对比着看了:
Snipaste_2021-02-20_16-50-01.webp