typescript

本文最后更新于:2022年5月9日 下午

语言类型

类型安全

  • 强类型 (不允许隐式类型转换)
  • 弱类型 (允许隐式转换)

类型检查

  • 静态类型语言 (变量类型书写时就确定且不能修改)

  • 动态类型语言 (变量类型运行时才能确定,且可以修改)

javascript

  • 弱类型
  • 动态类型
  • 脚本语言,无需编译

Flow

javascript类型检查器

JUMP TO:Flow

…略过

Typescript

  • javascript的超集
  • 加入ECMAScript新特性和类型系统
  • 最终会被编译回javascript

数据类型

原始值类型

  • string
  • number(NaN Infinity)
  • boolean
  • void
  • null
  • undefined
  • symbol

  • object
  • array
  • tuple
  • enum
  • function
  • any

类型推断

let num1 = 123;
// num1:number
num1 = "mmm"; // error

类型断言

const num1 = n1 as number;
const num2 = <number>n2;

interface 接口

interface Post {
	[prop: string]: string; // 动态成员
	content?: string; // 可选
	readonly author: string; // 只读
}

class 类

class Person {
	// 必须要类型声明
	public name: string; // 默认 共有属性
	private age: number; // 私有属性,子类不能访问
	protected gender: boolean; // 只能在子类中访问

	private /*默认public*/ constructor(name: string, age: number) {
		this.name = name;
		this.age = age;
	} // 设置private后不能在外部new

	static create(name: string, age: number) {
		return new Person(name, age); // 可以在这里创建实例
	}
}
interface Eat {
	eat(food: string): void;
}
interface Run {
	run(distance: number): void;
}
class Person implements Eat, Run {
	eat(food: string): void {
		console.log("吃了" + food);
	}
	run(distance: number): void {
		console.log("跑了" + distance);
	}
}

抽象类

只能被继承不能被new

abstract class Animal{
  eat(food:string):void{
    ...
  }
  abstract run (distance:number):void
}

class Cat extends Animal{
  // 提示 Non-abstract class 'Cat' does not implement inherited abstract member 'run' from class 'Animal'.
  // 需要自己写出符合run形状的run方法
  run(distance: number): void {
    throw new Error("Method not implemented.");
  }
}

泛型

function createArray<T>(length: number, value: T): T[] {
	const arr = Array<T>(length).fill(value);
	return arr;
}

类型声明

interface(接口) 和 type(类型别名)的区别

  1. interface不能实现联合类型number | string
  2. interface可以重复定义,且进行合并
  3. interface不能实现utility type

utility type

https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype

  • Partial
// 模拟
type Partial<T> = {
	[P in keyof T]?: T[P]; // 改成可选
};
  • Required
type Required<T> = {
	[P in keyof T]-?: T[P]; // 改成必选
};
  • Record
// 将K中的所有属性转为T类型
type Record<K extends keyof any, T> = {
	[P in K]: T;
};
  • Pick
type Pick<T, K extends keyof T> = {
	[P in K]: T[P]; // 选出你要的属性
};
  • Exclude
// 删除T中的U类型
type Exclude<T,U> = T extends U ? never : U
  • Extract
    // 提取T中的U类型

    type Extract<T, U> = T extends U ? T : never;
  • Omit

// 删除联合类型中的属性
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
  • NonNullable

    // 从T中排除null undefined
    type NumNullable<T> = Exclude<T,null|undefined>
  • Parameters

    // 获取函数参数类型
    type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
    
  • ReturnType

    // 获取函数返回类型
    type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;

typescript
http://yoursite.com/2022/03/30/typescript/
作者
tatekii
发布于
2022年3月30日
许可协议