Ts中的泛型函数总结keyof

keyof

TypeScript中的keyof操作符,是将一个类型映射为它所有成员名称的联合类型。

interface Person {
  name: string;
  age: number;
  gender: string;
}
type P = keyof Person; // "name" | "age" | "gender"

// 我们可以看到,keyof将Person这个对象类型映射成了一个联合类型
// 因此我们可以更方便的操作这个联合类型
Partial

Partial的作用是将传入的属性变成可选项,原理就是使用keyof拿到所有属性名,然后再使用in[遍历],T[P]拿到相应的值。

type Partial = { [P in keyof T]?: T[P] }
//作用:生成一个新类型,该类型与 T 拥有相同的属性,但是所有属性皆为可选项

看看使用实列

interface Foo {
    name: string
    age: number
}
type Bar = Partial
// 相当于
type Bar = {
    name?: string
    age?: number
}

Required

Required 的作用是将传入的属性变为必选项,原理是使用-?将可选项的?去掉。与之对应的还有个+?。

type Require = { [p in keyof T]-?: T[P] }

例子

interface Foo {
    name: string
    age?: number
}
type Bar = Required
// 相当于
type Bar = {
    name: string
    age: string
}
Readonly

Readonly的作用是将传入的属性变为只读选项

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

Mutable的作用是将传入属性的readonly移除。

This is useful as a lightweight builder for a type with readonly properties (especially useful when some of the properties are optional and conditional logic is needed before assignment).

type Mutable = {
  -readonly[P in keyof T]: T[P]
};
type Mutable = {-readonly[P in keyof T]: T[P]};
 interface Foobar {
   readonly a: number;
   readonly b: number;
   readonly x?: string;
 }

 function newFoobar(baz: string): Foobar {
   const foobar: Mutable = {a: 1, b: 2};
   if (shouldHaveAnX(baz)) {
     foobar.x = 'someValue';
   }

   return foobar;
 }

Record

Record的作用是将K中所有属性的值转化成T类型

Record构造具有给定类型T的一组属性K的类型。在将一个类型的属性映射到另一个类型的属性时,Record非常方便。他会将一个类型的所有属性值都映射到另一个类型上并创造一个新的类型.

type Record = {
    [P in K]: T;
};
type petsGroup = 'dog' | 'cat' | 'fish';
interface IPetInfo {
    name:string,
    age:number,
}

type IPets = Record;

const animalsInfo:IPets = {
    dog:{
        name:'dogName',
        age:2
    },
    cat:{
        name:'catName',
        age:3
    },
    fish:{
        name:'fishName',
        age:5
    }
}
Pick

Pick的作用是从T中取出一系列K的属性

type Pick = { [P in K]: T[P]; };
interface Person {
  name: string;
  age: number;
  id: number;
  sex: 0 | 1;
}

// 问女生年纪不太礼貌,所以我们不需要 age 这个属性
type Woman = Pick;

// 此时 Woman 等效于 Female

interface Female {
  name: string;
  id: number;
}
Exclude

Exclude的作用是从T中找出U中没有的元素

type Exclude = T extends U ? never : T;

type A = Exclude<'key1' | 'key2', 'key2'> 
// 'key1'

这个定义就利用了条件类型中的分配原则,来尝试将实例拆开看看发生了什么:

type A = `Exclude<'key1' | 'key2', 'key2'>`

// 等价于

type A = `Exclude<'key1', 'key2'>` | `Exclude<'key2', 'key2'>`

// =>

type A = ('key1' extends 'key2' ? never : 'key1') | ('key'2 extends 'key2' ? never : 'key2')

// =>

// never是所有类型的子类型
type A = 'key1' | never = 'key1'
Extract

高级类型Extract和上面的Exclude刚好相反,它是将第二个参数的联合项从第一个参数的联合项中提取出来,当然,第二个参数可以含有第一个参数没有的项。

下面是其定义和一个例子,有兴趣可以自己推导一下

type Extract = T extends U ? T : never 
type A = Extract<'key1' | 'key2', 'key1'> // 'key1'
Omit

Omit的作用是忽略对象的某些属性功能
它的作用主要是:以一个类型为基础支持剔除某些属性,然后返回一个新类型。

type Omit = Pick>;
type Person = {
    name: string;
    age: string;
    location: string;
};

type PersonWithoutLocation = Omit;

// PersonWithoutLocation equal to QuantumPerson
type QuantumPerson = {
    name: string;
    age: string;
};

你可能感兴趣的:(typescript,javascript,前端)