-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhouzhenpan
committed
Jul 29, 2024
1 parent
5c5bc56
commit 72f731f
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |