Skip to content

Commit

Permalink
feat: without
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzhenpan committed Jul 29, 2024
1 parent 5c5bc56 commit 72f731f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/05117-medium-without.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// ============= Test Cases =============
import type { Equal, Expect } from './test-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]>, []>>
]

// ============= Your Code Here =============

type ArrayToUnion<T extends number | number[]> = T extends number[]
? T[number]
: T

/** 遇到`U`可能是数组或数字的情况下,把数组转成Union类型,然后使用extends作为条件就很方便处理 */
type Without<
T,
U extends number | number[],
Result extends any[] = []
> = T extends [infer F, ...infer R]
? F extends ArrayToUnion<U>
? Without<R, U, Result>
: Without<R, U, [...Result, F]>
: Result

0 comments on commit 72f731f

Please sign in to comment.