首页 > 其他分享 >[Typescript] 91. Hard - Union to Intersection

[Typescript] 91. Hard - Union to Intersection

时间:2022-11-09 17:35:11浏览次数:63  
标签:Typescript _____________ Union true 42 91 foo type any

Implement the advanced util type UnionToIntersection<U>

For example

type I = Union2Intersection<'foo' | 42 | true> // expected to be 'foo' & 42 & true

 

https://www.cnblogs.com/Answer1215/p/16874524.html

/* _____________ Your Code Here _____________ */

type UnionToIntersection<U> = (U extends any ? (x: U) => any: never) extends 
  (x: infer R) => any ? R: never;



/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<UnionToIntersection<'foo' | 42 | true>, 'foo' & 42 & true>>,
  Expect<Equal<UnionToIntersection<(() => 'foo') | ((i: 42) => true)>, (() => 'foo') & ((i: 42) => true)>>,
]

 

标签:Typescript,_____________,Union,true,42,91,foo,type,any
From: https://www.cnblogs.com/Answer1215/p/16874548.html

相关文章