[Typescript Challenge] 148 Medium - CartesianProduct

Given 2 sets (unions), return its Cartesian product in a set of tuples, e.g.
  CartesianProduct<1 | 2, 'a' | 'b'>
  // [1, 'a'] | [2, 'a'] | [1, 'b'] | [2, 'b']

 

Solution:

/* _____________ Your Code Here _____________ */

type CartesianProduct<T, U> = T extends infer T1
  ? U extends infer U1
    ? [T1, U1]
    : never
  : never

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

type cases = [
  Expect<Equal<CartesianProduct<1 | 2, 'a' | 'b'>, [2, 'a'] | [1, 'a'] | [2, 'b'] | [1, 'b']>>,
  Expect<Equal<CartesianProduct<1 | 2 | 3, 'a' | 'b' | 'c' >, [2, 'a'] | [1, 'a'] | [3, 'a'] | [2, 'b'] | [1, 'b'] | [3, 'b'] | [2, 'c'] | [1, 'c'] | [3, 'c']>>,
  Expect<Equal<CartesianProduct<1 | 2, 'a' | never>, [2, 'a'] | [1, 'a'] >>,
  Expect<Equal<CartesianProduct<'a', Function | string>, ['a', Function] | ['a', string]>>,
]

 

posted @   Zhentiw  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-07-18 [SaSS] Using Object like style to create class dynamiclly
2018-07-18 [Vue-rx] Watch Vue.js v-models as Observable with $watchAsObservable and RxJS
2018-07-18 [Vue-rx] Pass Template Data Through domStreams in Vue.js and RxJS
2018-07-18 [Vue-rx] Disable Buttons While Data is Loading with RxJS and Vue.js
2018-07-18 [Vue-rx] Share RxJS Streams to Avoid Multiple Requests in Vue.js
2018-07-18 [Vue-rx] Switch to a Function which Creates Observables with Vue.js and Rxjs
2018-07-18 [Vue-rx] Handle Image Loading Errors in Vue.js with RxJS and domStreams
点击右上角即可分享
微信分享提示