typescript 接口、类、泛型、枚举、断言的简单使用

-

// 接口
export interface jobsType {
  id:string,
  name:string,
  age:number,
  job:string,
  [propname:string]: any // 代表可以包含任意的属性
}

export interface selectInt {
  WorkingYears:string[],
  Welfares:string[],
  PayMonths:string[],
  PayFilter:string[],
  [key:string]:string[]
}

export interface selectKeyOptionInt {
  key:string,
  name:string,
  inp:string
}
//
export class initData {
  jobs:Array<jobsType> = []; // 泛型
}

export interface optionTypeInt {
  [propname:string]:Array<string>[]
}

枚举

enum MSGG {
  "操作成功" = 200,
  "密码错误" = 201,
  "账号错误" = 202,
  "请求异常" = 999
}

断言

const getKey = (o:optionTypeInt, key: keyof typeof o):keyof typeof data.jobs => {
       return (o[key] as any); // 断言
     }

 

 

 

-

posted @ 2022-03-27 14:41  古墩古墩  Views(56)  Comments(0Edit  收藏  举报