[Typescript] 103. Hard - Tuple Filter

Implement a type FilterOut<T, F> that filters out items of the given type F from the tuple T.

For example,

type Filtered = FilterOut<[1, 2, null, 3], null> // [1, 2, 3]

 

/* _____________ Your Code Here _____________ */

type FilterOut<T extends any[], U, ACC extends any[] = []> = T extends [infer F, ...infer REST]
  ? [F] extends [U]
    ? FilterOut<REST, U, ACC>
    : FilterOut<REST, U, [...ACC, F]>
  : ACC;

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

type cases = [
  Expect<Equal<FilterOut<[], never>, []>>,
  Expect<Equal<FilterOut<[never], never>, []>>,
  Expect<Equal<FilterOut<['a', never], never>, ['a']>>,
  Expect<Equal<FilterOut<[1, never, 'a'], never>, [1, 'a']>>,
  Expect<Equal<FilterOut<[never, 1, 'a', undefined, false, null], never | null | undefined>, [1, 'a', false]>>,
  Expect<Equal<FilterOut<[number | null | undefined, never], never | null | undefined>, [number | null | undefined]>>,
]

 

posted @   Zhentiw  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2019-11-16 [Flutter] Style a message chat style-ish bubble
2019-11-16 [Flutter] Flexible the Widget height to available space
2019-11-16 [Flutter] Clear a TextField after send button
2019-11-16 [Flutter] Using StreamBuilder to build a List of Text Widget based on the data from Firebase
2016-11-16 [MobX] MobX fundamentals: deriving computed values and managing side effects with reactions
2014-11-16 [Grunt] External Config
2014-11-16 [Grunt] Uglify
点击右上角即可分享
微信分享提示