基本类型
1、
interface IBase {
num: number;
str: string;
bol: boolean;
nul: null;
und: undefined;
arr: any[];
ars: [string, number];
ner: never;
unk: unkonwn;
}
2、
enum Color {
Red,
Green,
Blue,
}
3、
function hello(): void {
alert('Hello');
}
使用方法
1、extends 继承
class Per {
name: string;
pwd: number;
tel: number;
constructor(name: string, pwd: number, tel: number) {
this.name = name;
this.pwd = pwd;
this.tel = tel;
}
login(): string {
if (this.name) {
return `${this.name}登陆成功了`;
}
return '登陆失败了';
}
}
const a1: Per = new Per('张三', 123, 110);
console.log('张三', a1.login());
class It extends Per {
constructor(name: string, pwd: number, tel: number, age: number) {
super(name, pwd, tel);
this.age = age;
}
age: number;
sayAge(): string {
return `我叫${this.name},今年${this.age}岁`;
}
changeName(newName: string): string {
return `我现在叫${newName}`;
}
}
const a2: It = new It('李四', 111, 120, 12);
console.log('李四', a2.login(), a2.sayAge(), a2.changeName('王五'));
2、定义剩余参数的写法
function fn(a: number, b: number, ...acc: number[]): number {
let to: number = a + b;
for (const num of acc) {
to += num;
}
return to;
}
console.log('fn:', fn(1, 2, 4));