# TypeScript 快速开始

# 1. 安装 TS 工具包

npm i -g typescript

# 查看 TS 版本
tsc -v

## Version 5.0.4

# 2. 编译并运行 TS 文件

编辑 hello.ts:

const sayHello = () => {
  const name: string = '张三';
  console.log(`hello, ${name}`);
}

sayHello();

编译 hello.ts:

  • 执行 tsc 命令

    tsc hello.ts
    
  • 生成 hello.js 文件

    var sayHello = function () {
        var name = '张三';
        console.log("hello, ".concat(name));
    };
    sayHello();
    

运行 hello.js:

node hello.js

# 3. ts-node

说明:

  • 直接运行 ts 文件
  • 内部将 ts 文件编译为 js 文件后,再执行 js 文件

安装:

npm i -g ts-node

ts-node -v
## v10.9.1

使用:

ts-node hello.ts
本章目录