promise all

2022-07-27  本文已影响0人  sweetBoy_9126

测试 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]
上一篇下一篇

猜你喜欢

热点阅读