type Includes<U, T> = U extends [infer F, ...infer Rest]
? Equal<F, T> extends true
? true
: Includes<Rest, T>
: false;
type Unique<T, A extends any[] = []> =
T extends [infer F, ...infer Rest]
? Includes<A, F> extends true // 检查 F 是否已经存在于累积数组 A 中
? Unique<Rest, A> // 如果存在,继续处理 Rest
: Unique<Rest, [...A, F]> // 如果不存在,将 F 加入 A
: A;
Solution by wendao-liu #35078
type Include<V extends any, F extends any[]> = F extends [infer R, ...infer rest] ? Equal<V, R> extends true ? true : Include<V, rest> : false
type Unique<T extends any[], U extends any[] = []> = T extends [infer R, ...infer rest] ? Include<R, U> extends true ? Unique<rest, U> : Unique<rest, [...U, R]> : U
Solution by ouzexi #34097
type isEqual<First, Second> = (<G>() => G extends First ? 1 : 2) extends (<G>() => G extends Second ? 1 : 2) ? true : false
type Includes<T extends any[], U> = T extends [infer First, ...infer Rest] ? true extends isEqual<First, U> ? true : Includes<Rest, U> : false
type Unique<T extends any[], Result extends any[] = []> = T extends [infer First, ...infer Rest]
? Includes<Result, First> extends false
? Unique<Rest, [...Result, First]>
: Unique<Rest, Result>
: Result
Solution by PiligrimStas #33779
type UniqueRest<T extends any[], K extends any> = T extends [
infer F,
...infer R
]
? Equal<F, K> extends true
? [...UniqueRest<R, K>]
: [F, ...UniqueRest<R, K>]
: [];
type Unique<T extends any[]> = T extends [infer F, ...infer R]
? [F, ...Unique<UniqueRest<R, F>>]
: [];
Solution by nuonuonuonuoyan #33442
思路:取出一个元素和剩下的列表进行对比,如果取出的元素在剩下的元素中,则递归,否则,加入当前元素,剩余元素继续递归,由于最终结果需要保持列表原有的顺序,需要从尾部向头部获取
// 你的答案
type isEqual<T, U> = (<G>() => G extends T ? 1:2) extends (<G>() => G extends U ? 1:2) ? true : false;
type Includes<T extends any[], U extends any> = T extends [infer F, ...infer R] ? isEqual<F, U> extends true ? true: Includes<R, U> : false;
type Unique<T extends any[]> = T extends [...infer R, infer Tail] ? Includes<R, Tail> extends true ? Unique<R> : [...Unique<R>, Tail] : T;
Solution by HelloGGG #33366
// your answers
type Unique<T, U = never> =
T extends [infer F, ...infer R]
? true extends (U extends U ? Equal<U, [F]> : never)
? Unique<R, U>
: [F, ...Unique<R, U | [F]>]
: []
Solution by pea-sys #33114
type IndexOf<
T extends unknown[],
U extends unknown,
C extends any[] = []
> = T extends [infer F, ...infer R]
? (<V>() => V extends F ? 1 : 0) extends <V>() => V extends U ? 1 : 0
? C["length"]
: IndexOf<R, U, [...C, any]>
: -1;
type Unique<T extends any[], R extends any[] = []> = T extends [
infer F,
...infer U
]
? IndexOf<R, F> extends -1
? Unique<U, [...R, F]>
: Unique<U, R>
: R;
Solution by vangie #32189
// Checks if array T includes element U
type Includes<T, U> = T extends [infer F, ...infer Rest]
? Equal<F, U> extends true
? true
: Includes<Rest, U>
: false;
type Unique<T extends any[], U extends any[] = []> =
T extends [infer F, ...infer R]
? Includes<U, F> extends true
? Unique<R, U>
: Unique<R, [...U, F]>
: U
Solution by matallui #30814
type Unique<T extends any[], R extends any[] = []> = T extends [infer A, ...infer B] ?
A extends R[number] ?
Unique<B, [...R]> : Unique<B, [...R, A]> : R;
type unique = Unique<[1, 1, 2, 2, 4, 4]>;
Solution by sundial-dreams #29558
type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B
? 1
: 2
? true
: false;
type Diff<T extends unknown[], Ele> = T extends [infer F, ...infer R]
? IsEqual<F, Ele> extends true
? false
: Diff<R, Ele>
: true;
type Unique<T, Res extends unknown[] = []> = T extends [
infer Ele,
...infer Rest
]
? Diff<Res, Ele> extends true
? Unique<Rest, [...Res, Ele]>
: Unique<Rest, Res>
: Res;
Solution by DoubleWoodLin #28782
// your answers
type Includes<T, U> = U extends [infer F, ...infer Rest]
? Equal<F, T> extends true
? true
: Includes<T, Rest>
: false;
type Unique<T, U extends any[] = []> =
T extends [infer R, ...infer F]
? Includes<R, U> extends true
? Unique<F, [...U]>
: Unique<F, [...U, R]>
: U
Solution by GreattitJY #28107
// 判断两个类型是否相等
type IsEqual<A, B> = (
// 判断类型T(数组类型)中是否包含类型E type Include<T, E> = T extends [infer F, ...infer R] ? IsEqual<F, E> extends true ? true : Include<R, E> : false;
// 定义一个U用于存储去重后的类型,递归遍历T,判断U中是否存在该类型(遍历到的),存在则“添加”进去 type Unique<T, U extends unknown[] = []> = T extends [infer F, ...infer R] ? Include<U, F> extends true ? Unique<R, U> : Unique<R, [...U, F]> : U;
Solution by jiaowoxiaobala #27880
1、判断传入的字段是否在数组中,完全相等
type EqArray<T extends unknown[], S> = Equal<T[0], S> extends true ? true : T extends [unknown, ...infer Next] ? EqArray<Next, S> : false
2、定义一个Result,用于存放唯一的每一个值,循环T,如果T中的第一项在Result中,则循环除第一项以外的后面,否则将当前项添加到Result中
type Unique<T extends unknown[], Result extends unknown[] = []> = T extends [infer Pre, ...infer Next]
? EqArray<Result, Pre> extends true
? Unique<Next, Result>
: Unique<Next, [...Result, Pre]>
: Result
Solution by jiechliu #27812
type Includes<T extends readonly any[], U> =
T extends [infer L, ...infer R] ? Equal<L, U> extends true ? true : Includes<R, U> : false
type Unique<T, U extends unknown[] = []> =
T extends [infer F, ...infer R] ? Unique<R, [...U, ...(Includes<U, F> extends true ? [] : [F])]> : U
Solution by smileboyi #27142
type Includes<T extends readonly unknown[], U> =
T extends [infer First, ...infer Rest]
? Equal<First, U> extends true ? true : Includes<Rest, U>
: false
type Unique<T extends unknown[]> = T extends [...infer Rest, infer Last]
? Includes<Rest, Last> extends true
? [...Unique<Rest>]
: [...Unique<Rest>, Last]
: T
Solution by HubooDeclan #27007
type SetArray<T,U> = T extends [infer R,...infer S] ? Equal<R,U> extends true ? false : SetArray<S,U> : true
type Unique<T,SetArr extends unknown[] = []> = T extends [infer R,...infer S] ? SetArray<SetArr,R> extends true ? Unique<S,[...SetArr,R]> : Unique<S,SetArr> : SetArr
Solution by WangZiChu199910252255 #26898
// 递归查找元素是否在数组中
type IsInclude<T extends unknown[], U> =
T extends [infer F, ...infer R]
? Equal<F,U> extends true
? true : IsInclude<R,U>
: false
type Unique<T extends unknown[], Res extends unknown[] = []> =
T extends [infer F, ...infer R]
? IsInclude<Res, F> extends true
? Unique<R, Res> : Unique<R, [...Res, F]>
: Res
Solution by liuk123456789 #26131
// 你的答案
type MyInclude<T extends unknown[], E extends unknown> =
T extends [infer X, ...infer Y]
? Equal<X, E> extends true
? true
: MyInclude<Y, E>
: false
type Unique<T extends unknown[], Cur extends unknown[] = []> =
T extends [infer X, ...infer Y]
? MyInclude<Cur, X> extends true
? Unique<Y, Cur>
: Unique<Y, [...Cur, X]>
: Cur
Solution by kiki-zjq #25709
Use 5153-IndexOf to determine whether F exists in Res, If F is in Res, it will not be added to Res, otherwise, it will be added to Res
// your answers
type Unique<T extends any[], Res extends any[] = []> = T extends [
infer F,
...infer Rest
]
? IndexOf<Res, F> extends -1
? Unique<Rest, [...Res, F]>
: Unique<Rest, Res>
: Res;
Solution by DvYinPo #25612
type isEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
? 1
: 2
? true
: false;
type Includes<T extends any[], U> = {
[K in keyof T]: isEqual<T[K], U>;
}[number] extends false
? false
: true;
type Unique<T extends any[], Result extends any[] = []> = T extends [
infer F,
...infer R
]
? Includes<Result, F> extends false
? Unique<R, [...Result, F]>
: Unique<R, Result>
: Result;
Solution by JohnLi1999 #24385
type Includes<T extends unknown[], U> = T extends [infer First, ...infer Rest]
? (
Equal<First, U> extends true
? true
: Includes<Rest, U>
)
: false
type Unique<T extends unknown[], Acc extends unknown[] = []> = T extends [infer First, ...infer Rest]
? Includes<Acc, First> extends true
? Unique<Rest, Acc>
: Unique<Rest, [...Acc, First]>
: Acc
Solution by NeylonR #24370
// your answers
type Includes<T extends any[], U> = T extends [infer Left, ...infer Rest] ? Equal<Left, U> extends true ? true : Includes<Rest, U>: false
type Unique<T extends any[], Res extends any[] = []> = T extends [infer Left, ...infer Rest]
? Includes<Res, Left> extends true
? Unique<Rest, Res>
: Unique<Rest, [...Res, Left]>
: Res
Solution by studymachiney #24227
type IndexOf<T extends any[], U> = T extends [infer F, ...infer R] ? Equal<F, U> extends true ? T[`length`] : IndexOf<R, U> : -1;
type Unique<T extends any[], _Collector extends any[] = []> = T extends [infer F, ...infer R] ? Unique<R, IndexOf<_Collector, F> extends -1 ? [..._Collector, F] : _Collector> : _Collector/*return*/;
// old way
// type IndexOf<T extends any[], U, _Counter extends any[] = []> =
// T extends [] ? -1 :
// (
// Equal<U, T[0]> extends true ?
// _Counter["length"] :
// (
// T extends [any, ...infer R] ? IndexOf<R, U, [..._Counter, unknown]> : T
// )
// );
// type Unique<T extends any[], _Counter extends any[] = []> =
// T extends [infer F, ...infer R] ?
// (
// IndexOf<_Counter, F> extends -1 ? Unique<R, [..._Counter, F]> : Unique<R, _Counter>
// ) :
// _Counter;
Solution by E-uler #23991
type Exsit<T extends any[], U> = T extends [infer F, ...infer R]
? Equal<F, U> extends true
? true
: Exsit<R, U>
: false;
type Unique<T> = T extends [...infer F, infer R]
? Exsit<F, R> extends true
? Unique<F>
: [...Unique<F>, R]
: [];
Solution by BarrySong97 #23702
type Exist<T, U> = T extends [infer F, ...infer R] ? Equal<F, U> extends true ? true : Exist<R, U> : -1
type Unique<T, Acc extends unknown[] = []> = T extends [infer F, ...infer R] ?
Exist<Acc, F> extends -1 ? Unique<R, [...Acc, F]> : Unique<R, [...Acc]> : Acc
Solution by snakeUni #23163
// your answers
type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
type Include<A, F> = A extends [infer First, ...infer Rest] ? IsEqual<First, F> extends true ? true : Include<Rest, F> : false;
type Unique<
T extends unknown[],
Acc extends unknown[] = []
> = T extends [infer First, ...infer Rest]
? Include<Acc, First> extends true
? Unique<Rest, Acc>
: Unique<Rest, [...Acc, First]>
: Acc
Solution by jxhhdx #22746
// 是否全等
type IsEqual<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false
// U是否存在于T中
type Includes<T extends readonly any[], U> =
T extends [infer F, ...infer L]
? IsEqual<U,F> extends true
? true
: Includes<L,U>
: false
type Unique<T extends any[], U extends any[] = []> =
T extends [infer F, ...infer L]
? Includes<U,F> extends true ? [...Unique<L,[...U]>] : [F,...Unique<L,[...U,F]>]
: T
Solution by TKBnice #22282
type Without<T extends unknown[],U,S extends unknown[] = [] > = T extends [infer A,...infer B]
?
(A extends U ? Without<B,U,S> : Without<B,U,[...S,A]>)
:
S
type Unique<T extends unknown[],U extends unknown[] = []> = T extends [infer A,...infer B] ? Unique<Without<B,A>,[...U,A]> : U
Solution by tororo00 #22252
type Unique<T, U = never> =
T extends [infer F, ...infer R]
? true extends (U extends U ? Equal<U, [F]> : never)
? Unique<R, U>
: [F, ...Unique<R, U | [F]>]
: []
Iterates over T
by one item
U
- the union of already included items wrapped into arrays. E.g. for T
equals ['a', string, ...]
, on step 2 the U
would be ['a'] | [string]
. We need to wrap items into arrays because otherwise 'a' | string
would collapse into string
Solution by Alexsey #21766
type Includes<T extends unknown[], U> = T extends [infer Start, ...infer End] ?
Equal<Start, U> extends true ? true: Includes<End, U> : false;
type Unique<T extends unknown[], R extends unknown[] = []> = T extends [infer Start, ...infer End] ?
Unique<End, [...R,...(Includes<R, Start> extends false ? [Start] : [])]> : R
Solution by Karamuto #21510