TypeScript 是 JavaScript 的一个超集。
ts更容易发现代码的问题;语法提示更加完善;语义化更强,代码的可读性更高
ts环境的搭建
1.安装node
2.npm install nrm -g 安装nrm
3.npm install typescript -g 安装ts
运行ts代码
type Point = { x: number, y: number };
function a(point1: Point, point2: Point) {
return [point1.x + point2.x, point1.y - point2.y];
}
const [x, y] = a({ x: 1, y: 3 }, { x: 2, y: 0 });
console.log(x, y);
ts代码在游览器和node中是无法识别的,需要转化成js代码
可以在终端下运行 tsc xxx.ts node xxx.js 就可以查看
这种比较麻烦还可以在安装 ts-node 然后运行ts-node xxx.ts
如果运行报错
Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include
安装 npm install -g @types/node 即可
ts 具有静态校验能力,在代码还没有执行的时候就可以进行错误预警