将联合类型转换为相交类型

TypeScript

Me无敌

2020-05-25

有没有一种方法可以将联合类型转换为交叉类型:

type FunctionUnion = () => void | (p: string) => void
type FunctionIntersection = () => void & (p: string) => void

我想申请一个转型FunctionUnion来获得FunctionIntersection

第4173篇《将联合类型转换为相交类型》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

2个回答
humengqiao 2021.07.14

type FunctionIntersection<T extends (arg?: any) => void> = (T extends (arg?: any) => void ? (arg: T) => void : never) extends ((arg: infer P) => void) ? P : never

 

type T = FunctionIntersection<FunctionUnion>

 

T为(() => void) & ((p: string) => void)交叉类型

 

上面打错字了!

humengqiao 2021.07.14

type FunctionIntersection<T extends (arg?: any) => void> = (T extends (arg?: any) => void ? (arg: T) => void : never) extends ((arg: infer P) => void) ? P : never

 

type T = FunctionIntersection<FunctionUnion>

 

T为(() => void) & ((p: string) => void)联合类型

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android