高级类型

2021-02-10  本文已影响0人  bestCindy

这是我的笔记,原文: https://segmentfault.com/a/1190000023800536

泛型

泛型就是在编写代码的时候不指定类型,在实例化时候把类型作为参数指明类型

泛型通过一对尖括号来表示(<>),尖括号内的字符被称为类型变量,这个变量用来表示类型

目的是提高代码的复用性

function fn<T>(args: T): T {
    return args;
}
const str = fn<string>("This is Cindy");
console.log(str);//This is Cindy

交叉类型(&)

就是将多个类型合并成一个类型

T & U
interface Button {
    type: string,
    text: string
}

interface Link {
    alt: string,
    href: string
}

const linkBtn: Button & Link = {
    type: "link",
    text: "跳转",
    alt: "跳转",
    href: "http://www.baidu.com"
}

联合类型(|)

表示链接的多个类型中的任意一个

T | U
interface Button {
    type: "default" | "primary" | "danger",
    text: string
}

const btn: Button = {
    type: "primary",
    text: "button"
}

类型别名

当用联合类型或者交叉类型约束一个类型的时候,可以给这个类型取一个名字

type Alias = T | U
type InnerType = "default" | "primary" | "danger";

interface Button {
    type: InnerType,
    text: string
}

类型索引(keyof)

用于获取一个接口中 key 的联合类型

interface Button {
    type: string,
    text: string
}

type ButtonKeys = keyof Button;
// 等价于
// type ButtonKeys = "type" | "text"
interface ButtonStyle {
    color: string,
    background: string
}

interface ButtonTypes {
    default: ButtonStyle,
    primary: ButtonStyle,
    danger: ButtonStyle
}

interface Button {
    type: "default" | "primary" | "danger",
    text: string
}

// 注意 使用 keyof

interface Btn {
    type: keyof ButtonStyle,
    text: string
}

类型约束(extends)

注意这个不是继承用的,是对类型加个约束

type BaseType = string | number | boolean

// 这里的 T 只能是 BaseType 里面的类型
function fn<T extends BaseType>(arg: T): T {
    return arg;
}

一般与 keyof 一起用

比如,有一个方法专门用来获取对象的值,但是这个类型并不确定

function getValue<T, K extends keyof T>(obj: T, key: K) {
    return obj[key]
}

const obj = { a: "1" }
const a = getValue(obj, "a");

类型映射(in)

可以对接口的 key 或者联合类型做个遍历操作

type Readonly<T> = {
    readonly [P in keyof T]: T[P];
}

interface Obj {
    a: string,
    b: string
}

type ReadOnlyObj = Readonly<Obj>

// type ReadOnlyObj = {
//     readonly a: string,
//     readonly b: string
// }

条件类型(U ? X :Y)

T extends U ? X : Y

如果 T 中的类型在 U 存在,则返回,否则抛弃

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

Partial

用于将一个接口的所有属性设置为可选状态

type Partial<T> = {
    [P in keyof T]: T[P]
}

Required

作用和 Partial 相反,就是把所有属性改为必须的

type Required<T> = {
    [P in keyof T]-?: T[P]
}

Record

生成的类型具有类型 K 中存在的属性,值为 T

type Record<K extends keyof any,  T> = {
    [P in K]: T
}

Exclude

如果 T 中的类型在 U 中不存在,则返回,否则抛弃

type Exclude<T, U> = T extends U ? never : T

Pick

用于提取接口的某几个属性

type Pick<T, K extends keyof T> = {
    [P in K]: T[P]
}
interface Todo {
    title: string,
    complete: boolean,
    description: string
}

type TodoPreview = Pick<Todo, "title" | "complete">

const todo: TodoPreview = {
    title: "Clean room",
    complete: false
}

Omit

和 Pick 作用相反

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>
interface Todo {
    title: string,
    complete: Boolean,
    description: string
}

type TodoPreview = Omit<Todo, "description">

const todo: TodoPreview = {
    title: "Clean room",
    complete: false
}
上一篇 下一篇

猜你喜欢

热点阅读