交叉类型

同时满足两种类型(几种类型之和) (&)

  1. function extend<T extends Object, U extends Object>(
  2. source: T,
  3. target: U
  4. ): T & U {
  5. let result = {} as T & U;
  6. for (let key in source) {
  7. result[key] = source[key] as any;
  8. }
  9. for (let key in target) {
  10. if (!result.hasOwnProperty(key)) {
  11. result[key] = target[key] as any;
  12. }
  13. }
  14. return result;
  15. }
  1. class Person {
  2. name: string;
  3. constructor(name: string) {
  4. this.name = name;
  5. }
  6. }
  7. interface Logable {
  8. log(): void;
  9. }
  10. class Log implements Logable {
  11. log() {
  12. console.log('log');
  13. }
  14. }
  15. const test = extend(new Person('james'), new Log());
  16. // test 满足Person,Log两种类型
  17. test.name = 'curry';
  18. test.log();

联合类型

几种类型之一 (|)

  1. function test(value: string | string): number {
  2. if (typeof value === 'number') {
  3. return value;
  4. }
  5. if (typeof value === 'string') {
  6. return parseFloat(value);
  7. }
  8. return 0;
  9. }
  10. class Dog {
  11. name: string;
  12. constructor(name: string) {
  13. this.name = name;
  14. }
  15. run() {}
  16. }
  17. class Fish {
  18. name: string;
  19. constructor(name: string) {
  20. this.name = name;
  21. }
  22. swim() {}
  23. }
  24. function getPet(name: string, isDog: boolean): Dog | Fish {
  25. if (isDog) {
  26. return new Dog(name);
  27. } else {
  28. return new Fish(name);
  29. }
  30. }

类型保护

方法一: is XXX

  1. // error:类型“Dog | Fish”上不存在属性“run”。
  2. // dog.run()
  3. // 如何确定具体是什么类型:类型保护
  4. function isDog(pet: Fish | Dog): pet is Dog {
  5. return (pet as Dog).run !== undefined;
  6. }
  7. if (isDog(pet)) {
  8. pet.run();
  9. } else {
  10. pet.swim();
  11. }

方法二:使用 typeof、instanceof 进行类型保护

  1. if (pet instanceof Dog) {
  2. pet.run();
  3. }

null

tsc xxx.ts —strictNull (如果空值,会提示报错)

  1. class C {
  2. a: number;
  3. b?: number;
  4. constructor(a: number, b?: number) {
  5. this.a = a;
  6. this.b = b;
  7. }
  8. }
  9. const c = new C(1, 2);
  10. c.a = 20;
  11. // 不能将类型“null”分配给类型“number | undefined”。
  12. // c.b = null

变量后加 !:表示变量一定不为 null
变量后加 ?:表示变量可能为空,编译时加了判断

  1. function broken(name: string | null) {
  2. function getFirstChar() {
  3. // return name?.charAt(0)
  4. return name!.charAt(0);
  5. }
  6. name = name || 'default';
  7. return getFirstChar();
  8. }
  1. // 编译结果
  2. function broken(name) {
  3. function getFirstChar() {
  4. // void 0 (相当于 undefined)
  5. return name === null || name === void 0 ? void 0 : name.charAt(0);
  6. }
  7. name = name || 'default';
  8. return getFirstChar();
  9. }

undefined

  1. let a: string = 'str';
  2. // error:不能将类型“undefined”分配给类型“string”
  3. a = undefined;

字符串字面量

例如这种情况,ease 只能是:'easi-in'|'ease-out'|'ease-in-out'

type Easing = 'ease-in' | 'ease-out' | 'ease-in-out';
class UIElement {
  animate(dx: number, dy: number, easing: Easing) {
    if (easing === 'ease-in') {
    } else if (easing === 'ease-out') {
    } else if (easing === 'ease-in-out') {
    }
  }
}