1.安装TypeScript

npm install -g typescript

编译一个TypeScript 文件

tsc hello.ts

2.将以下代码 写入到 hello.ts 中

1
2
3
4
5
function sayHello(person:string) {
return `hello ${person}`;
}
let user:string='Tom';
sayHello(user);

然后执行

tsc hello.ts

这时候会生成一个编译好的文件 hello.js

1
2
3
4
5
function sayHello(person) {
return `hello ${person}`;
}
let user = 'Tom';
sayHello(user);
  • TypeScript 使用 : 指定变量的类型
  • TypeScript 只会进行静态检查,如果发现有错误,编译的时候就会报错