type IsEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
type All<T extends any[], U> =
T extends [infer F, ...infer Rest]
? IsEqual<F, U> extends true
? All<Rest, U>
: false
: true;
Solution by wendao-liu #35111
type All<T extends any[], L> = T extends [infer R, ...infer rest] ? (Equal<R, L> extends true ? All<rest, L> : false) : true
Solution by ouzexi #34143
// your answers
type All<A extends unknown[], Elt> =
Equal<Equal<A[number], A[0]> & Equal<A[0], Elt>, true>
Solution by pea-sys #33471
type All<A extends unknown[], Elt> =
Equal<Equal<A[number], A[0]> & Equal<A[0], Elt>, true>
Solution by YusukeTakeuchi #32917
type All<T extends any[], U> = T["length"] extends 0
? false
: T[number] extends U
? true
: false;
Solution by amirabbasajoudani #32641
type All<T extends any[], R, S = R> = Equal<T[number], R> extends true
? [R] extends [never]
? true
: R extends R
? [S] extends [R]
? true
: false
: never
: false;
Solution by vangie #32192
type All<T extends any[], N> = T[number] extends N ? true : false;
Solution by rkamely #32103
type All<T, U> = T extends [infer A, ...infer B] ? Equal<A, U> extends true ? All<B, U> : false : true
Solution by dreamluo-plus #30690
// your answers
// 借用一下Equal,比较F和U,相等就一直递归返回true,否则是false
type All<T extends unknown[], U> = T extends [infer F, ...infer R]
? Equal<F, U> extends true
? All<R, U>
: false
: true
Solution by bebusy007 #30032
// your answers
type All<T extends unknown[], S> = T extends [infer F , ...infer Rest] ? F extends S ? All<Rest, S> : false : true
Solution by kerolossamir165 #29963
应该是比较简单的解法
type All<T extends unknown[], Target> = Equal<T[number], Target>
Solution by stevenaces #29518
type IsEqualType<T, U> = (<G>() => G extends T ? true : false) extends <G>() => G extends U ? true : false ? true : false;
type All<T extends any[], E = any> = T extends [infer F, ...infer L]
?
IsEqualType<F, E> extends true
?
All<L, E>
: false
: true
Solution by dmytro-shumak #29243
type IsEqual<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B
? 1
: 2
? true
: false;
type All<T extends unknown[], U extends unknown> = T extends [
infer Item,
...infer Rest
]
? IsEqual<Item, U> extends true
? All<Rest, U>
: false
: true;
Solution by DoubleWoodLin #28892
type All<T extends unknown[], U> = T extends [infer I, ...infer Rest] ? Equal<I, U> extends true ? All<Rest, U> : false : true
Solution by IvanKoigerov #28880
type All<ArrayType extends unknown[], Type> = ArrayType extends [
infer Head,
...infer Tail
]
? [Head] extends [Type]
? [Type] extends [object]
? [Head] extends [object]
? All<Tail, Type>
: false
: [Head] extends [object]
? false
: All<Tail, Type>
: false
: true;
Solution by yevhenpavliuk #28498
这题常规递归判断每一个值也可以解,通过 infer 取数据的每一位,判断是否和 K 相同,如果有不同就返回 false
下面用了一个工具方法 Equal,可以自己实现。
type All<T extends any[], K> = T extends [infer F, ...infer Rest]
? Equal<F, K> extends true
? All<Rest, K>
: false
: true
在 github 上还看到了,这样的解答,很有意思
直接将数组转成联合类型,利用联合类型的遍历特性和 N 逐个比较。
type All<T extends any[], N> = T[number] extends N ? true : false;
Solution by linjunc #28341
type IsEqual<A, B> = (<X>() => X extends A ? 1 : 2) extends <X>() => X extends B
? 1
: 2
? true
: false;
// 思路:构建两个类型"变量",TLen存T的长度,R存每次递归满足条件的成员
type All<
T extends unknown[],
U,
TLen = T["length"],
R extends unknown[] = []
> = T extends [infer F, ...infer Rest]
? // 如果有一个成员与U不相等,则直接返回false
IsEqual<F, U> extends true
? // 否则添加进R
All<Rest, U, TLen, [...R, unknown]>
: false
: // 递归结束时,比较原先T的长度和R的长度
IsEqual<R["length"], TLen>;
Solution by jiaowoxiaobala #27987
type Equal<T, U> = (<G>() => G extends T ? 1 : 2) extends <
G
>() => G extends U ? 1 : 2
? true
: false;
type All<T extends any[], U> = T extends [infer F, ...infer R]
? Equal<F, U> extends true
? All<R, U>
: false
: true;
Solution by de-novo #27853
type All<T extends unknown[], S> = Equal<S, T[number]> extends true ? true : false
Solution by smileboyi #27272
type All<T extends unknown[], U> = T extends [infer First, ...infer Rest]
? (<G>() => G extends First ? 1 : 2) extends <G>() => G extends U ? 1 : 2
? Rest extends []
? true
: All<Rest, U>
: false
: never;
Solution by rldnd #27011
type All<T extends any[], U> = Equal<T[number], U> extends true ? true : false
Solution by owenvip #26799
type Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false
type All<T extends unknown[], A extends unknown> = T extends [infer X, ...infer Tail] ? Eq<A,X> extends true ? All<Tail,X> : false : true
Solution by ScarboroughCoral #26571
// your answers
type All<T extends any[], U> = T extends [infer F, ...infer Rest]
? (<T>() => T extends F ? 1 : 2) extends <T>() => T extends U ? 1 : 2
? All<Rest, U>
: false
: true;
Solution by DvYinPo #26170
type All<T extends unknown[], U> = T extends [infer Head, ...infer Tail] ? Equal<Head, U> extends true ? All<Tail, U> : false: true;
Solution by kanishev #25917
// your answers
type All<T extends unknown[], K, Flag extends boolean = false> = T extends [infer Left, ...infer Rest]
? Equal<Left, K> extends true
? All<Rest, K, true>
: false
: Flag
Solution by studymachiney #24745
type All<A extends any[], T> =
A extends (infer I)[] ?
Equal<I, T>
: never
;
Solution by MrNinso #24675
type All<T extends any[], U> =
[T[number]] extends [U] ?
(
true extends IsAny<T[number]> | IsAny<U> ? //其中有一个是any
true extends IsAny<T[number]> & IsAny<U> ? true : false : // 两个都是any ? true : false
true
) :
false;
// simple way
// type All<T extends any[], U> = Equal<T[number], U>;
// old way
// type ToUnion<T extends any[]> = T extends [infer F, ...infer R] ? F | ToUnion<R> : never;
// type All<T extends any[], U> = T extends []/*optional*/ ? false : Equal<U, ToUnion<T>>;
Solution by E-uler #24560
type All<T extends any[],U> = [{
[K in keyof T]:Equal<T[K],U>
}[number] ] extends [true]? true:false
Solution by jiangshanmeta #24477
~~type All<T extends unknown[], U> = Equal<T[number], U>~~
type All<T extends unknown[], U> = T extends [infer First, ...infer Rest]
? Equal<First, U> extends true
? All<Rest, U>
: false
: true
Solution by NeylonR #24412
type All<T extends unknown[], P> = T[number] | P extends P ? true : false
Solution by zhuizhui0429 #24190