[Typescript] 70. Medium - Without
Implement the type version of Lodash.without, Without<T, U> takes an Array T, number or array U and returns an Array without the elements of U.
type Res = Without<[1, 2], 1>; // expected to be [2]
type Res1 = Without<[1, 2, 4, 1, 5], [1, 2]>; // expected to be [4, 5]
type Res2 = Without<[2, 3, 2, 3, 2, 3, 2, 3], [2, 3]>; // expected to be []
/* _____________ Your Code Here _____________ */
type Without<T extends any[], U extends any[] | any> = T extends [infer F, ...infer RT]
? U extends any[] // U is array like
? F extends U[number] // F in U array
? Without<RT, U> // exlcude F
: [F, ...Without<RT, U>] // include F
: F extends U // U is not an array like
? Without<RT, U> // exlcude F
: [F, ...Without<RT, U>] // include F
: [];
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Without<[1, 2], 1>, [2]>>,
Expect<Equal<Without<[1, 2, 4, 1, 5], [1, 2]>, [4, 5]>>,
Expect<Equal<Without<[2, 3, 2, 3, 2, 3, 2, 3], [2, 3]>, []>>,
]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-10-26 [CSS] Use CSS Transforms to Create Configurable 3D Cuboids
2020-10-26 [CSS] Use CSS Variables Almost like Boolean Values with Calc (maintainable css)
2020-10-26 [Kotlin] Typecheck with 'is' keyword, 'as' keyword for assert type
2020-10-26 [Kotlin] When to add () and when not to
2020-10-26 [Kotlin] fold / reduce
2016-10-26 [CSS] Control Image Aspect Ratio Using CSS (object-fit)
2016-10-26 [Angular2 Form] Reactive form: valueChanges, update data model only when form is valid