typescript高级类型语法

& (交叉类型)

&让两者的类型都满足才行,否则报错:

interface Bird {
  name: string;
  fly(): void;
}
interface Per {
  name: string;
  talk(): void;
}

type BirdPerson = Bird & Per;

let p: BirdPerson = { name: "zhufeng", fly() {}, talk() {} };

let p1: BirdPerson = { name: "zhufeng", fly() {} };//缺少talk()

|(联合类型 )

即满足其中一个类型即可:

type Day = number | string;

let oneDay: Day = 1;
let twoDay: Day = '2';
let threeDay: Day = {};// 报错=> 不能将类型“{}”分配给类型“Day

Pick(提取关键字)

Pick:从T中提取U属性,返回取到的所有属性。

// Pick提取关键字
type Person1 = Pick; //=> { name: string; age: number; }

let p2:Person1 = { name: "小明", age: 18 }; //编译成功
let p3:Person1 = { name: "小明", age: '12121' }; //编译失败  age 不能将类型“string”分配给类型“number”
let p4:Person1 = { name: "小明" }; //编译失败  缺少age属性

Record(记录 )

Record:把T变成K的所有属性的值类型。

// Record 记录
interface PageInfo {
  title: string;
  description: string;
}

type Page = "home" | "about" | "contact";

// type MyRecord = { [P in T]: U };
type TypeTodo1 = Record; //=> { home: PageInfo; about: PageInfo; contact: PageInfo }

const todoType: TypeTodo1 = {
  home: {
    title: "121",
    description: "1212",
  },
  about: {
    title: "121",
    description: "1212",
  },
  contact: {
    title: "121",
    description: "1212",
  },
}; //=> 编译成功

const todoType1: TypeTodo1 = {
  home: {
    title: "121",
    description: "1212",
  },
  about: {
    title: "121",
    description: "1212",
  },
  contact: {
    title: "121",
  },
}; //=> 编译失败  缺少属性 "description",但类型 "PageInfo" 中需要该属性。


type perf=Record;//会被编译成下面的 perf1 

type perf1 = {
  [x: string]: any;
}

Omit(反向获取) 

Omit :从T中剔除掉U属性,返回剩下的所有属性。

interface Person {
  name: string;
  age: number;
}

// Omit反向获取,取除了age之外的
type Person2 = Omit; //=> { name: string; }

let Per:Person2 = { name: '小明' }; //编译通过
let Per1:Person2 = { age: 11 }; //编译失败 不能将类型“{ age: number; }”分配给类型“Person2”

Exclude(排除) 

Exclude :从T中剔除可以赋值给U的类型。

type E = Exclude; // 排除关系 输出 number;

let n: E = 1111; //编译成功
let n1: E = '1111'; //编译失败 不能将类型“string”分配给类型“number”。

Extract(包含)

Extract :提取T中可以赋值给U的类型。

type I = Extract; // 包含关系 输出 string;

let i: I = '1111'; //编译成功
let ii: I = 1111; //编译失败 不能将类型“number”分配给类型“string”。

Partial(部分)

Partial :返回所有属性变成可选的,即属性可传可不传。

// Partial 部分
type Animal = {
  species: string;
  age: number;
};

type Cat = Partial; //=>{ species?: string; age?: number; }
let cat: Cat = {
  species: "猫科动物"
}; //编译成功
let cat1: Cat = {}; //编译成功

keyof (索引类型查询操作符)

keyof T :获取 T上已知的公共属性名的联合。

type Animal = {
  species: string;
  age: number;
};

type Dog = keyof Animal;// 'species' | 'age'
let dog: Dog = "species"; //编译成功
let dog1: Dog = "age"; //编译成功
let dog2: Dog = "name"; //编译失败  不能将类型“"name"”分配给类型“keyof Animal”。

NonNullable(非空)

NonNullable : 从T中剔除nullundefined

type N = NonNullable; // 删除null和undifined;
let nm: N = 111; 编译成功
let nm1: N = '1112'; //编译成功
let nm2: N = null; //编译失败

ReturnType(函数返回值类型) 

ReturnType获取函数返回值类型

function getUserInfo(name: string, age: number) {
  return { name, age };
}
// ReturnType获取函数返回值
type UserInfo = ReturnType; //{ name: string; age: number; }

const userA: UserInfo = {
  name: "zhufeng",
  age: 10,
}; //编译成功

const userA1: UserInfo = {
  name: "zhufeng"
}; //编译失败  中缺少属性 "age"

Parameters(函数参数类型)

Parameters :获取函数所有参数的类型元组

// 获取函数所有参数的类型元组
type T1 = Parameters;//  [name: string, age: number]
let t: T1 = ["1111", 111];

InstanceType(实例类型)

InstanceType : 获取构造函数类型的实例类型

class Poit {
  x:number = 0;
  y:string = '';
}

type T20 = InstanceType;  // { x:number; y:string };

let T2:T20={x:111,y:'1111'};

你可能感兴趣的:(typeScript,typescript)