美文网首页
promise all

promise all

作者: sweetBoy_9126 | 来源:发表于2022-07-27 16:20 被阅读0次

测试 case

const promiseAllTest1 = PromiseAll([1, 2, 3] as const)
const promiseAllTest2 = PromiseAll([1, 2, Promise.resolve(3)] as const)
const promiseAllTest3 = PromiseAll([1, 2, Promise.resolve(3)])

type cases = [
  Expect<Equal<typeof promiseAllTest1, Promise<[1, 2, 3]>>>,
  Expect<Equal<typeof promiseAllTest2, Promise<[1, 2, number]>>>,
  Expect<Equal<typeof promiseAllTest3, Promise<[number, number, number]>>>
]

知识点

  1. {[P in keyof T]: T[P] }
    declare function A<T extends any>(value: T): { [P in keyof T]: T[P] }
    1). 对于数组 返回的是一个数组
A([1, 2, 3]) // number[]

2). 对于对象返回的是一个对象

A({ a: 1, b: 2, c: 3 }) // {a: number, b: number, c: number}
  1. [...T]
    解构数组的每一项,T 就会变成每一项的类型就相当于 T extends [...infer U] => U
declare function P<T extends unknown[]>(value: [...T]): T
P([1, 2, 3]) // [number, number, number]
  1. as const
    // 如果类型里面用了 as const 那说明你这个类型是只读类型,类型接收的时候就要加 readonly
PromiseAll([1, 2, 3] as const)
values: readonly [...T]

相关文章

网友评论

      本文标题:promise all

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