1.类型别名 type
type Name = string;type NameResolver = () => string;type NameOrResolver = Name | NameResolver;function getName(n: NameOrResolver): Name {if (typeof n === 'string') {return n;} else {return n();}}
2.字符串字面量类型
type EventNames = 'click' | 'scroll' | 'mousemove';
3.元组
数组合并了相同类型的对象,而元组(Tuple)合并了不同类型的对象。
let tom: [string, number] = ['Tom', 25];//给元组赋值可只赋值其中一项,但是当直接对元组类型的变量进行初始化或者赋值的时候,需要提供所有元组类型中指定的项。//当添加越界的元素时,它的类型会被限制为元组中每个类型的联合类型。
4.枚举
enum Days {Sun, Mon, Tue, Wed, Thu, Fri, Sat};console.log(Days["Sun"] === 0); // trueconsole.log(Days["Mon"] === 1); // trueconsole.log(Days["Tue"] === 2); // trueconsole.log(Days["Sat"] === 6); // trueconsole.log(Days[0] === "Sun"); // trueconsole.log(Days[1] === "Mon"); // trueconsole.log(Days[2] === "Tue"); // true//枚举编译后var Days;(function (Days) {Days[Days["Sun"] = 0] = "Sun";Days[Days["Mon"] = 1] = "Mon";Days[Days["Tue"] = 2] = "Tue";Days[Days["Wed"] = 3] = "Wed";Days[Days["Thu"] = 4] = "Thu";Days[Days["Fri"] = 5] = "Fri";Days[Days["Sat"] = 6] = "Sat";})(Days || (Days = {}));
1.手动赋值
enum Days {Sun = 7, Mon = 1, Tue, Wed, Thu, Fri, Sat};console.log(Days["Sun"] === 7); // trueconsole.log(Days["Mon"] === 1); // trueconsole.log(Days["Tue"] === 2); // trueconsole.log(Days["Sat"] === 6); // true//手动赋值的枚举项也可以为小数或负数,此时后续未手动赋值的项的递增步长仍为 1:enum Days {Sun = 7, Mon = 1.5, Tue, Wed, Thu, Fri, Sat};console.log(Days["Sun"] === 7); // trueconsole.log(Days["Mon"] === 1.5); // trueconsole.log(Days["Tue"] === 2.5); // trueconsole.log(Days["Sat"] === 6.5); // true
5.类
1.public
修饰的属性或方法是公有的,可以在任何地方被访问到,默认所有的属性和方法都是
class Animal {public name;public constructor(name) {this.name = name;}}let a = new Animal('Jack');console.log(a.name); // Jacka.name = 'Tom';console.log(a.name); // Tom
2.private
使用 private 修饰的属性或方法,在子类中也是不允许访问的
3.protected
如果是用 protected 修饰的属性或方法,则允许在子类中访问:
4.修饰构造函数
当构造函数修饰为 private 时,该类不允许被继承或者实例化;
当构造函数修饰为 protected 时,该类只允许被继承:
5.抽象类
abstract 用于定义抽象类和其中的抽象方法。
1. //首先,抽象类是不允许被实例化的:abstract class Animal {public name;public constructor(name) {this.name = name;}public abstract sayHi();}let a = new Animal('Jack');// index.ts(9,11): error TS2511: Cannot create an instance of the abstract class 'Animal'.
2. //其次,抽象类中的抽象方法必须被子类实现:abstract class Animal {public name;public constructor(name) {this.name = name;}public abstract sayHi(); //抽象方法}class Cat extends Animal {public eat() {console.log(`${this.name} is eating.`);}}let cat = new Cat('Tom');// index.ts(9,7): error TS2515: Non-abstract class 'Cat' does not implement inherited abstract member 'sayHi' from class 'Animal'.
6.类的类型
class Animal {name: string;constructor(name: string) {this.name = name;}sayHi(): string {return `My name is ${this.name}`;}}let a: Animal = new Animal('Jack');console.log(a.sayHi()); // My name is Jack
6.类与接口
1.implements 类实现接口
实现(implements)是面向对象中的一个重要概念。一般来讲,一个类只能继承自另一个类,有时候不同类之间可以有一些共有的特性,这时候就可以把特性提取成接口(interfaces),用 implements 关键字来实现。这个特性大大提高了面向对象的灵活性。
interface Alarm {alert(): void;}class Door {}class SecurityDoor extends Door implements Alarm {alert() {console.log('SecurityDoor alert');}}class Car implements Alarm {alert() {console.log('Car alert');}}//一个类可以实现多个接口interface Alarm {alert(): void;}interface Light {lightOn(): void;lightOff(): void;}class Car implements Alarm, Light {alert() {console.log('Car alert');}lightOn() {console.log('Car light on');}lightOff() {console.log('Car light off');}}
2.接口继承接口
interface Alarm {alert(): void;}interface LightableAlarm extends Alarm {lightOn(): void;lightOff(): void;}
3.接口继承类
class Point {x: number;y: number;constructor(x: number, y: number) {this.x = x;this.y = y;}}interface Point3d extends Point {z: number;}let point3d: Point3d = {x: 1, y: 2, z: 3};
7.泛型
泛型(Generics)是指在定义函数、接口或类的时候,不预先指定具体的类型,而在使用的时候再指定类型的一种特性。
//定义时未指定返回值类型,而根据输入的参数value的类型自动推断function createArray<T>(length: number, value: T): Array<T> {let result: T[] = [];for (let i = 0; i < length; i++) {result[i] = value;}return result;}createArray<string>(3, 'x'); // ['x', 'x', 'x']
