TypeScript 入门
TypeScript 安装
安装 tsc,它是 TypeScript 的编译器。
npm i -g typescript
查看版本
tsc -v
Version 5.5.3
安装 ts-node,它是 TypeScript 的运行时。
npm i -g ts-node
查看版本
ts-node -v
v10.9.2
Hello World
使用 tsc --init 命令快速创建一个 tsconfig.json 文件。
tsc --init
Created a new tsconfig.json with:
TS
target: es2016
module: commonjs
strict: true
esModuleInterop: true
skipLibCheck: true
forceConsistentCasingInFileNames: true
You can learn more at https://aka.ms/tsconfig
创建一个 hello.ts 文件。
function hello(name: string) {
console.log(`Hello, ${name}!`);
}
hello("TypeScript");
使用 tsc 命令编译 hello.ts 文件。
tsc hello.ts
编译后生成一个 hello.js 文件。