美文网首页
07-TypeScirpt-映射类型-分布式条件类型

07-TypeScirpt-映射类型-分布式条件类型

作者: 低头看云 | 来源:发表于2020-10-04 08:00 被阅读0次

映射类型

  • 根据旧的类型创建出新的类型, 我们称之为映射类型
interface TestInterface1 {
    name: string
    age: number
  }
  interface TestInterface2 {
    readonly name: string
    readonly age: number
  }

  // type Readonly<T> = {
  //   readonly [P in keyof T]: T[P]
  // }
  // type Partial<T> = {
  //   [P in keyof T]?: T[P]
  // }
  // 像下面这样使用:

  type PersonPartial = Partial<TestInterface1>
  type ReadonlyPerson = Readonly<TestInterface1>
  // 可读  可选参数
  type MyType1 = Partial<Readonly<TestInterface1>>
  • Pick映射类型
    • 将原有类型中的部分内容 映射到新的类型中
 interface TestInterface {
    name: string
    age: number
  }

  //type MyType2 = {
  // name: string;
  // }
  type MyType2 = Pick<TestInterface, 'name'>

  • Record 映射类型
    • 会将一个类型的所有属性值都映射到另一个类型上 并创建一个新的类型
  type Animal = 'person' | 'dog' | 'cat'
  interface PropInterface {
    name: string
    age: number
  }
  type MyType3 = Record<Animal, PersonPartial>

  let pp: MyType3 = {
    person: {
      name: 'zs',
      age: 18,
    },
    dog: {
      name: 'wc',
      age: 3,
    },
    cat: {
      name: 'mm',
      age: 2,
    },
  }

条件类型(三目运算)

  • 判断前面一个类型时候是后面一个类型或者继承于后面一个类型
  • 如果是就返回第一个结果, 如果不是就返回第二个结果
    • 语法 T extends U ? X : Y
  type MyType<T> = T extends string ? string : any
  type res = MyType<boolean>

分布式条件类型

  • 被检测类型是一个联合类型的时候, 改条件类型就被称之为分布式条件类型
  type MyType1<T> = T extends any ? T : never
  type MyType2 = MyType1<string | number | boolean | null | undefined>

  • 提取T中可以赋值给U的类型。 Extract
  type MyType3 = Extract<MyType2, string>
  • 从T 中剔除可以赋值给U的属性 Exclude
  type MyType4 = Exclude<MyType2, string>
  • 从T 中剔除 null 和undefined属性 NonNullable
  type MyType5 = NonNullable<MyType2>
  • 获取函数返回值类型 ReturnType
  type MyType6 = ReturnType<() => number> // number
  • 获取一个类的构造函数参数组成的元祖类型
  class Person {
    constructor(public name: string, public age: number) {}
  }

  type MyType7 = ConstructorParameters<typeof Person>
  • 获取函数的参数类型组成的元祖类型 Parameter\s
  function say(name: string, age: number, str: string) {}

  type MyType8 = Parameters<typeof say>

  // Parameters
  type Parameters<T extends (...args: any) => any> = T extends (
    ...args: infer P
  ) => any
    ? P
    : never

infer 关键字

  • 条件类型提供了一个infer关键字, 可以让我们在条件类型中定义新的类型

  • 需求: 定义一个类型, 如果传入的是数组, 就返回数组的元素类型

  • 如果传入是普通类型, 则直接返回这个类型

  type MyType11<T> = T extends Array<infer U> ? U : T

  type MyType12 = MyType<number>
  type MyType13 = MyType<number[]>
  type MyType14 = MyType<string[]>
  • 解释
  type ParamType<T> = T extends (param: infer P) => any ? P : T
  // 这个条件语句 T extends (param: infer P) => any ? P : T 中,infer P 表示待推断的函数参数。
  // 整句表示为:如果 T 能赋值给 (param: infer P) => any,则结果是 (param: infer P) => any 类型中的参数 P,否则返回为 T。

  interface User {
    name: string
    age: number
  }

  type Func = (user: User) => void

  type Param = ParamType<Func> // Param = User
  type AA = ParamType<string> // string
  

相关文章

网友评论

      本文标题:07-TypeScirpt-映射类型-分布式条件类型

      本文链接:https://www.haomeiwen.com/subject/psguuktx.html