// string & unknown ηεδΊ string
type Intersection<T extends any[]> = T extends [infer R, ...infer rest] ? (R extends any[] ? R[number] : R) & Intersection<rest> : unknown
Solution by ouzexi #34307
type Intersection<T extends any[]> = T extends [infer L, ...infer R]
? L extends any[]
? L[number] & Intersection<R>
: L & Intersection<R>
: unknown;
// θΏιζεδΈδΈͺεζ―εΊθ―₯ζ― unknownοΌε δΈΊunknownεδ»»δ½η±»εεδΊ€εη±»ει½ηδΊθ―₯η±»ε
type Result1 = 1 & unknown // 1
type Result2 = any & unknown // any
type Result3 = {} & unknown // {}
Solution by Vampirelee #32646
type Normalize<T> = T extends any[] ? T[number] : T
type Intersection<T extends any[]> = T extends [infer Head, ...infer Tail]
? Tail['length'] extends 0
? Normalize<Head>
: Normalize<Head> & Intersection<Tail>
: never
Solution by mrf1freak #26948
type Intersection<T> = T extends [infer F, ...infer R]
? Intersection<R> & (F extends unknown[] ? F[number] : F)
: unknown;
Solution by JohnLi1999 #25898
type Intersection<T extends any[]> = T extends [infer F, ...infer R] ?
(F extends any[] ? F[number] : F) & Intersection<R> :
unknown;
// old way
// type ToUnion<T extends any[]> = T[number];
// type Intersection<T extends any[]> = T extends [infer F, ...infer R] ?
// (F extends any[] ? ToUnion<F> : F) & Intersection<R> :
// unknown;
Solution by E-uler #24985
type Intersection<T extends unknown[]> = T extends [ infer First, ...infer Rest]
? First extends unknown[]
? First[number] & Intersection<Rest>
: First & Intersection<Rest>
: unknown
Solution by NeylonR #24535
// your answers
type GetUnionIntersection<T, U> = T extends U ? T : never
type Intersection<T extends any[], Result = any> =
T extends [infer F, ...infer R]
? Intersection<R, GetUnionIntersection<F extends number[] ? F[number] : F,Result>>
: Result
Solution by snakeUni #24032
type GetUnionIntersection<T, U> = T extends U ? T : never
type Intersection<T extends any[], Result = any> =
T extends [infer F, ...infer R]
? Intersection<R, GetUnionIntersection<F extends number[] ? F[number] : F,Result>>
: Result
Solution by TKBnice #23374
type Intersection<T extends unknown[], R = unknown> =
T extends [infer A, ...infer Rest]
? Intersection<Rest, R & (A extends unknown[] ? A[number] : A)>
: R
Solution by drylint #22832
type TupleToUnion<T> = T extends unknown[] ? T[number] : T
type Intersection<T> =
T extends [infer First, ...infer Rest]
? Rest extends []
? TupleToUnion<First>
: Extract<TupleToUnion<First>, Intersection<Rest>>
: never
Solution by zhaoyao91 #21988
// your answers
type Intersection<T, U = ''>
= T extends [infer A, ... infer B]
? A extends any[]
? Intersection<[A[number], ...B], U>
: Intersection<B, (U extends '' ? A : A & U)>
: U
Solution by goddnsgit #21923
// your answers
type Intersection<T extends any[], Ret = T[0]> = T extends [infer First, ...infer Rest] ? Intersection<Rest, First extends any[] ? First[number] & (Ret extends any[] ? Ret[number] : Ret) : First & Ret> : Ret
Solution by Rebornjiang #21806
// your answers
type Intersection<T extends unknown[]> =
T extends [ infer F, ...infer Rest]
? F extends unknown[]
? F[number] & Intersection<Rest>
: F & Intersection<Rest>
: unknown
Solution by YqxLzx #21676
type Intersection<T extends Array<any>, ExtractValue = unknown> = T extends [
infer A,
...infer B
]
? A extends Array<any>
? Intersection<B, Extract<A[number], ExtractValue>>
: Intersection<B, Extract<A, ExtractValue>>
: ExtractValue
Solution by so11y #21288
type Intersection<T extends Array<any>, ExtractValue = unknown> = T extends [
infer A,
...infer B
]
? A extends Array<any>
? Intersection<B, Extract<A[number], ExtractValue>>
: Intersection<B, Extract<A, ExtractValue>>
: ExtractValue
Solution by so11y #21287
type Intersection<T extends unknown[]> = T extends [infer H, ...infer R]
? H extends readonly any[]
? H[number] & Intersection<R>
: H & Intersection<R>
: number;
Solution by JohannesSchwegler #20555
type Intersection<T extends unknown[]> =
T extends [ infer F, ...infer Rest]
? F extends unknown[]
? F[number] & Intersection<Rest>
: F & Intersection<Rest>
: unknown
Solution by lvjiaxuan #20335
type ExculdeReversal<First, End> = First extends any[]
? End extends any[]
? Exclude<First[number], Exclude<First[number], End[number]>>
: Exclude<First[number], Exclude<First[number], End>>
: End extends any[]
? Exclude<First, Exclude<First, End[number]>>
: Exclude<First, Exclude<First, End>>;
type Intersection<T extends unknown[], Last = unknown> = T extends [infer F, infer M, ...infer R]
? Intersection<R, ExculdeReversal<F, M>>
: T extends [infer F1, ...infer R1]
? ExculdeReversal<Last, F1>
: Last;
Solution by CaoXueLiang #19486
type Intersection_Help_ArrayToUnion<T> = T extends any[] ? T[number] : T
type Intersection<T> = T extends [infer F, ...infer R]
? Intersection_Help_ArrayToUnion<F> & Intersection<R>
: unknown
Solution by WongYAQi #17493
type TupleToUnion<T extends any[]> = T extends [infer el, ...infer rest] ? el | TupleToUnion
Solution by my5201314zwl #16866
// your answers
type Intersection<T> = T extends [infer F, ...infer R]
? F extends unknown[]
? Extract<F[number], Intersection<R>>
: Extract<F, Intersection<R>>
: unknown
Solution by humandetail #16496
// your answers
type Intersection<
T extends readonly (number|number[])[],
U extends number = number > =
T extends [infer R extends number[],...infer S extends (number|number[])[]]
? Intersection<S,Extract<R[number],U>>
: T extends [infer R extends number,...infer S extends (number|number[])[]]
? Intersection<S,Extract<R,U>>
: U ;
Solution by justBadProgrammer #16002
// your answers
type ArratToUnion<T extends unknown[], R extends unknown[] = []> = T extends [infer First, ...infer Rest]
? First extends unknown[]
? ArratToUnion<Rest, [...R, First[number]]>
: ArratToUnion<Rest, [...R, First]>
: R
type X = ArratToUnion<[[1, 2], [3, 4], [5, 6]]>
type X1 = ArratToUnion<[[1, 2, 3], 2 | 3 | 4, 2 | 3]>
type Intersection<T extends unknown[]> = ArratToUnion<T> extends [infer First, infer Second, infer Third]
? First extends Second
? First extends Third
? First
: never
: never
: never
Solution by liuxing95 #13155
// your answers
type ArrToUnion<T extends unknown[], R extends unknown[] = []> = T extends [infer First, ...infer Rest]
? [First] extends [unknown[]]
? ArrToUnion<Rest,[...R, First[number]]>
: ArrToUnion<Rest,[...R, First]>
: R
type Intersection<T extends unknown[]> = ArrToUnion<T> extends [infer First, infer Second, infer Third]
? First extends Second
? First extends Third
? First
: never
: never
: never
Solution by GumplinGo #13042
// your answers
type Intersection<T> = T extends [infer First, ...infer Rest]
? (First extends unknown[] ? First[number] : First) & Intersection<Rest>
: unknown;
Solution by SeptEarlyMorning #12399
type Intersection<T> = {[I in keyof T]: (_: T[I] extends (infer V)[] ? V : T[I]) => 1} extends
| Record<number, (_: infer I) => 1> ? I : never;
Solution by teamchong #11756
type Extract<T> = T extends unknown[] ? T[number] : T
type Intersection<T extends unknown[]> = T extends [infer Head, ...infer Rest]
? Extract<Head> & Intersection<Rest>
: unknown
Solution by adnasa #10717
type Intersection<T, Res = undefined>
=
T extends [infer Start,...infer Other] ?
Start extends any[] ? Res extends undefined ? Intersection<Other,Start[number]>: Intersection<Other, Extract<Res, Start[number]> >
:
Intersection<Other, Extract<Res, Start> >: Res
Solution by EGyoung #8551
type Intersection<T extends unknown[]> = T extends [infer F, ...infer R]
? (F extends unknown[] ? F[number] : F) & Intersection<R>
: unknown
Solution by LoTwT #7780
// your answers
// ε΅ε₯ηζ°η»η±»εε±εΌθ½¬ζunionη±»εοΌη»ζδΈδΈͺε
η΄ ζ―unionηζ°η»
type convertUnionArray<T extends unknown[], ResultArr extends unknown[] = []> =
T extends [infer First, ...infer Rest]
? First extends unknown[]
? convertUnionArray<Rest, [...ResultArr, First[number]]>
: convertUnionArray<Rest, [...ResultArr, First]>
: ResultArr
// δΊ€εη±»ε
type Intersection<T extends any[], Union = convertUnionArray<T>[0]> =
convertUnionArray<T> extends [infer First, ...infer Rest]
? Intersection<Rest, Extract<First, Union>>
: Union
Solution by HongxuanG #7343