typescript

命令

安装

1
npm install -g typescript

编译为js

1
2
tsc hello.ts //不指定名字的话生成hello.js
tsc hello.ts -w //使用watch mode

基本类型

Number Boolean String Symbol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
let num = 1; //不能num = "1" 已经规定了是number类型
let is = true;
let name = "name;
let count: number = 10;
let isDone: boolean = false;
let name: string = "semliker";
const sym = Symbol();
let obj = {
[sym]: "semlinker",
};
console.log(obj[sym]); // semlinker
Array Object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let arr = [1,2,3] //不能arr[0] = 'a'或者arr.push('a')
let list: number[] = [1, 2, 3];
let netja = {
name: 'aaa',
age: 0
}
//不能添加属性,比如netja.skills = ['x'],或者给不符合属性类型的值,比如netja.name = 1
//可以重新赋值
netja = {
name: 'xxx',
age: 9
}