type Filter<T extends any[], P, A extends any[] = []> = T extends [infer F, ...infer Rest]
? F extends P
? Filter<Rest, P, [...A, F]>
: Filter<Rest, P, A>
: A;
Solution by wendao-liu #35112
type Filter<T extends any[], P, U extends any[] = []> = T extends [infer R, ...infer rest] ? R extends P ? Filter<rest, P, [...U, R]> : Filter<rest, P, U> : U
Solution by ouzexi #34145
type Filter<T extends any[],U, A extends any[] = []> = T extends [infer S, ...infer rest] ? S extends U ? [S, ...A]:Filter<rest, U>:never
Solution by rookiewxy #33916
// your answers
type Filter<T extends any[], P> = T extends [infer L, ...infer R]
? [...L extends P ? [L] : [], ...Filter<R, P>]
: []
Solution by pea-sys #33495
type Filter<T, Condition, Ret extends any[] = []> = T extends [
infer First,
...infer Rest
]
? Filter<Rest, Condition, First extends Condition ? [...Ret, First] : Ret>
: Ret;
Solution by sunupupup #33477
// your answers
type Filter<T extends any[], P> = T extends [infer Item, ...infer Rest]
? [...Item extends P ? [Item] : [], ...Filter<Rest, P>]
: []
Solution by DevilTea #33259
type Filter<T extends unknown[], P> = T extends [infer F, ...infer R]
? F extends P
? [F, ...Filter<R, P>]
: Filter<R, P>
: T
Solution by keyurparalkar #32781
type Filter<T extends any[], P, Acc extends any[] = []> = T extends [infer F, ...infer Rest]
? F extends P
? Filter<Rest, P, [...Acc, F]>
: Filter<Rest, P, Acc>
: Acc
Solution by ZhulinskiiDanil #32724
type Filter<T extends any[], P, R extends any[] = []> = T["length"] extends 0
? R
: T[0] extends P
? Filter<T extends [any, ...infer U] ? U : never, P, [...R, T[0]]>
: Filter<T extends [any, ...infer U] ? U : never, P, R>;
Solution by vangie #32193
type Filter<T extends any[], P, Result extends T[number][] = []> = T extends [infer First, ...infer Rest]
? First extends P ? Filter<Rest, P, [...Result, First]> : Filter<Rest, P, Result>
: Result
Solution by hwasurr #31430
type Filter<T extends any[], P, U extends any[] = []> = T extends [infer A, ...infer B] ? A extends P ? Filter<B, P, [...U, A]> : Filter<B, P, U> : U
Solution by dreamluo-plus #30691
// your answers
// ไธไธชไธช็ญ๏ผๆณไบไธคไธชๆนๅผ
// ้คไบๅ ไธไธชUๅ่ฎฐๅฝ๏ผไนๅฏไปฅ็ดๆฅ่ฟๅๆฐ็ป
// type Filter<T extends unknown[], P, U extends unknown[] = []> = T extends [
// infer F,
// ...infer R
// ]
// ? F extends P
// ? Filter<R, P, [...U, F]>
// : Filter<R, P, U>
// : U
type Filter<T extends unknown[], P> = T extends [infer F, ...infer R]
? F extends P
? [F, ...Filter<R, P>]
: [...Filter<R, P>]
: []
Solution by bebusy007 #30031
type Filter<T extends unknown[], K> = T extends [infer F, ...infer R]
? F extends K
? [F, ...Filter<R, K>]
: Filter<R, K>
: [];
Solution by kanishev #29881
// your answers
type Filter<T extends any[], P> =
T extends [infer F , ...infer Rest] ?
F extends P ? [F, ...Filter<Rest, P>]
: Filter<Rest, P>
: []
Solution by kerolossamir165 #29626
type Filter<T extends any[], P> = T extends [infer Item, ...infer Rest]
? Item extends P
? [Item, ...Filter<Rest, P>]
: Filter<Rest, P>
: [];
Solution by DoubleWoodLin #28895
type Filter<ArrayType extends unknown[], Type> = ArrayType extends [
infer Head,
...infer Tail
]
? [...(Head extends Type ? [Head] : []), ...Filter<Tail, Type>]
: [];
Solution by yevhenpavliuk #28499
่ฟ้ขๅไธไธ้ขๅพ็ฑปๅ๏ผๅบๆฌไธๆ ท๏ผ้่ฟๆทปๅ ๅคไธไธชๅๆฐ Res ๆฅๅญๅจ่ฟๅ็ปๆ๏ผ้ๅ็ๆ Res ๆฐ็ป
type Filter<T extends any[], P, Res extends any[] = []> = T extends [infer F, ...infer Rest]
? F extends P
? Filter<Rest, P, [...Res, F]>
: Filter<Rest, P, Res>
: Res
ๅจ github ไธ่ฟ็ๅฐไบ่ฟไธชๅ็ญ๏ผๅฏไปฅ็ดๆฅๅจๆฐ็ปไธญ้ๅฝ๏ผ่ฟๆ ทๅฏไปฅๅๅฐๆ Res ๅๆฐ
type Filter<T extends unknown[], P> = T extends [infer F, ...infer R]
? F extends P
? [F, ...Filter<R, P>]
: Filter<R, P>
: [];
Solution by linjunc #28342
type Filter<T extends any[], P> = T extends [infer F, ...infer R]
? F extends P
? [F, ...Filter<R, P>]
: Filter<R, P>
: [];
Solution by slemchik03 #27575
type Filter<T extends any[], P> = T extends [infer F, ...infer R] ? F extends P ? [F, ...Filter<R, P>] : Filter<R, P> : [];
Solution by RainbowIsPerfect #27427
type Filter<T extends any[], P> =
T extends [infer F, ...infer R] ? [...(F extends P ? [F] : []), ...Filter<R, P>] : []
Solution by smileboyi #27273
type Filter<T extends unknown[], P, Result extends unknown[] = []> = T extends [infer First, ...infer Rest]
? [First] extends [P]
? Filter<Rest, P, [...Result, First]>
: Filter<Rest, P, Result>
: Result;
Solution by rldnd #27015
This works with duplicates as well.
type Filter<T extends any[], P, Acc extends any[] = []> = T extends [
infer F,
...infer L
]
? F extends P
? Filter<L, Exclude<P, F>, [...Acc, F]>
: Filter<L, P, Acc>
: Acc;
Solution by tany1 #26714
type Filter<T extends unknown[], P extends unknown> = T extends [infer X, ...infer Tail] ? X extends P ? [X, ...Filter<Tail,P>] : Filter<Tail,P> : []
Solution by ScarboroughCoral #26572
type Filter<TArray extends any[], TTarget> = TArray extends [
infer Head,
...infer Tail,
]
? Head extends TTarget
? [Head, ...Filter<Tail, TTarget>]
: [...Filter<Tail, TTarget>]
: []
Solution by valentynpodkradylin #26265
// your answers
type Filter<T extends any[], P, Res extends any[] = []> = T extends [
infer F,
...infer Rest
]
? F extends P
? Filter<Rest, P, [...Res, F]>
: Filter<Rest, P, Res>
: Res;
Solution by DvYinPo #26173
// your answers
type Filter<T extends any[], P, Res extends unknown[] = []> =
T extends [infer X, ...infer Y]
? X extends P
? Filter<Y, P, [...Res, X]>
: Filter<Y, P, Res>
: Res
Solution by kiki-zjq #25847
// your answers
type Filter<T extends any[], P, Res extends unknown[] = []> = T extends [infer Left, ...infer Rest]
? Left extends P
? Filter<Rest, P, [...Res, Left]>
: Filter<Rest, P, Res>
: Res
Solution by studymachiney #24945
type Filter<T extends any[], P> = T extends [infer F, ...infer R] ? [...(F extends P ? [F] : []), ...Filter<R, P>] : [];
// old way
// type Filter<T extends any[], P> = T extends [infer F, ...infer R] ? (F extends P ? [F, ...Filter<R, P>] : Filter<R, P>) : [];
Solution by E-uler #24561
type Filter<T extends any[], P,R extends any[] = []> = T extends [infer F,...infer L]? Filter<L,P,F extends P?[...R,F]:R>:R
Solution by jiangshanmeta #24478
type Filter<T extends unknown[], P> = T extends [infer A, ...infer rest] ? [...(A extends P ? [A] : []), ...Filter<rest, P>] : [];
Solution by sabercc #24439