ts的几种类型

124 阅读3分钟

联合类型

a: string|number
  • 查看类型提示:只会提示string和number上都有的属性
  • 如果使用a.length则会报错,因为length是string类型上才有的方法

image.png

image.png

所以需要使用到类型断言

keyof关键词返回的是联合类型

  • 取interface的键,就像属性组成的数组一样
  • 定义常量类型 image.png

相当于:

let key: 'area'|'name'|'population';

如何理解?

就像使用const定义变量,str变量的类型是'123'而不是string,如下图:

image.png

使用let定义的就是string类型,如下图:

image.png

再比如定义常量类型;

type PollingStatus = 'success' | 'failure' | 'next';

/**
* 轮训函数数据接口
* @param type 轮训处理状态: 'success' 成功| 'failure' 失败 | 'next' 继续轮训;
* @param data? 请求结果 
*/

export interface PollingStatus {
 type: PollingStatus;
 data?: any;
}

ts源码案例: 内置Partial类型的实现方法

/**
 * Make all properties in T optional
 */
type Partial<T> = {
    [P in keyof T]?: T[P];
};

image.png

类型断言:as关键词

  • 类型断言操作符
  • 参考:类型断言
  • 结合.tsx文件使用,与尖括号类型断言行为是等价的。因为在.tsx文件中使用尖括号运行会有困难。
// 告诉ts我知道a是string类型,可以使用length方法
function getLength(input: string | number) {
  const str = input as string; //类型断言为string
  if (str.length) {
    return str.length;
  } else {
    const num = input as number; //类型断言为number
    return num.toString().length;
  }
}
let foo = <foo>bar
let foo = bar as foo
enum  ConfigPng {
    item1 = 123,
    item2 = 455,
    item3 = ‘time’
}
ConfigType[cyclePromoScene as keyof typeof ConfigType]
//意思是已经断言 cyclePromoScene为ConfigPng属性中其中一个类型

交叉类型: |关键词

interface CountryTypes {
 name?:string;
}
type a =  string | CountryTypes

合并类型: &关键词

将多个接口或类型合并为一个接口,扩展具体的接口类型

type PropsWithChildren<P> = P & { children?: ReactNode }; 

lookup types

结合type定义别名的关键字,也可以通过接口和接口的键定义类型

image-20211120210820160.png

mapped types:in关键词

  • 循环属性
interface CountryResp {
  name: string;
  area: number;
  population: number;
}

type Keys = keyof CountryResp;

type MapTypes = {
  [key in Keys]: any;
};

image.png

案例让属性都变为可选

interface CountryResp {
  name: string;
  area: number;
  population: number;
}

type Keys = keyof CountryResp;

const key: Keys = "area";
// looks type
const name: CountryResp["name"] = "123";
// mapped type
type mappedTypes = {
  [P in Keys]?: CountryResp[P];
};

枚举类型:enum关键词

  • 约束数字按照名称递增(默认值0)
enum  ConfigPng {
    item1 = 123,
    item2 = 455,
    item3 = ‘time’
}
console.log(ConfigPng.item1,Config.item2); //获取成员变量
  • 直接使用ConfigPng[a]会一直报预发错误,虽然代码可以运行
  • 解决方法:as keyof typeof RightsTypes
export enum ConfigType {
  CUSTOMER_ENROLL = '会员',
  FIRST_ORDER_PROMO = '顾客',
  REPURCHASE_INCREASE = '普客',
  NON_PURCHASER_AROUSE = '邀约客',
}
ConfigType[cyclePromoScene as keyof typeof ConfigType]
  • any
  • void:函数返回值为undefined以外都不行
  • object
    • 对象或接口的可选属性
    • ? 问号,属性的定义:问号表示可选的属性,一般用于属性定义
interface Test {
  color?: string;   //color的值为string|undefined
  width: string
}

conditional types条件类型:extends关键字

IfAny案例:检查T是不是any类型,如果是返回Y,不是返回N

export type IfAny<T, Y, N> = 0 extends 1 & T ? Y : N;
type IsAny<T> = IfAny<T, true, false>;
const yes: IsAny<any> = true;
const no: IsAny<string> = false;

练习题测试:IsTypeEqual

// IsNotAny的定义就是IfAny定义的反向操作
export type IsNotAny<T> =   0 extends 1 & T ? true : true;
/**
 * Checks if T1 equals to T2.
 */
export type IsTypeEqual<T1, T2> = IsNotAny<T1> extends false ? false : (
    IsNotAny<T2> extends false ? false : (
        [T1] extends [T2] ? ([T2] extends [T1] ? true : false): false
    )
);