Skip to content

ts 安装

1. 执行安装命令

js
$ npm install -g typescript
// 或
$ yarn global add typescript

2. 查看版本号

安装完成后我们可以使用 tsc 命令来执行 TypeScript 的相关代码,以下是查看版本号:

js
$ tsc -v
> Version 4.3.5

3. 创建ts文件

通常我们使用 .ts 作为 TypeScript 代码文件的扩展名。 新建一个 index.ts 文件 书写以下代码

ts
var msg:string = "Hello World" 
console.log(msg)

4. 编译ts文件

执行以下命令将 TypeScript 转换为 JavaScript 代码:

js
tsc index.ts

这时候在当前目录下(与 index.ts 同一目录)就会生成一个 index.js 文件,代码如下

js
var msg = "Hello World" 
console.log(msg)

4. 运行

1. 运行 编译后的js 文件

js
node index.js

2. 直接运行ts 文件

  1. 下载ts 执行工具
js
npm i ts-node -g
  1. 执行运行命令
js
ts-node index.ts

Released under the MIT License.