简介

tsc

shell
npm install -g typescript
shell
tsc -v
作用
检查类型和语法错误,
提前纠错
ts的类型

如何穿件带有ts的vue工程

作用

常见类型

用法
编写一个ts文件
js
let username:string = "John";
let age:number = 25;
let isUpdated:boolean = true;
let data:any = "Hello World!";
console.log(username);
console.log(age);
console.log(isUpdated);
console.log(data);
编译
shell
tsc .\tsDemo.ts
效果
对应的ts文件下多了一个js文件
- t
s文件是不能直接运行的,只有编译成js才能运行
这是编译之后的js文件
js
var username = "John";
var age = 25;
var isUpdated = true;
var data = "Hello World!";
console.log(username);
console.log(age);
console.log(isUpdated);
console.log(data);
约束字面量类型(类似于enum枚举)

js
myPrintFunction("Hello World!","center");
myPrintFunction("Hello World!","left");
myPrintFunction("Hello World!","right");
function myPrintFunction(content:string,alignment:'right'|'left'|'center'):void {
console.log(content,alignment);
}