将联合类型转换为交叉类型是否有方法将联合类型转换为交叉类型:type FunctionUnion = () => void | (p: string) => voidtype FunctionIntersection = () => void & (p: string) => void我想将转换应用到FunctionUnion得到FunctionIntersection
3 回答
![?](http://img1.sycdn.imooc.com/5458453d0001cd0102200220-100-100.jpg)
德玛西亚99
TA贡献1770条经验 获得超3个赞
9
type Idx<T extends any, K extends keyof any, Yes> = T extends Record<K, any> ? T[K] & Yes : unknown type ArrayToIntersection<T extends any[]> = Idx<T, '0', Idx<T, '1', Idx<T, '2', Idx<T, '3', Idx<T, '4', Idx<T, '5', Idx<T, '6', Idx<T, '7', Idx<T, '8', Idx<T, '9', unknown>>>>>>>>>>// Usage: => 1 & 2 & 3 & 4type Result = ArrayToIntersection<[1, 2, 3, 4]>
![?](http://img1.sycdn.imooc.com/54586431000103bb02200220-100-100.jpg)
牛魔王的故事
TA贡献1830条经验 获得超3个赞
type Param<T> = T extends (arg: infer U) => void ? U : never;
type InferredParams = Param<((a: string) => void) | ((a: number) => void)>;
string & number
string | number
添加回答
举报
0/150
提交
取消