00191-medium-append-argument

Back

type AppendArgument<Fn extends (...arg: any[]) => unknown, A> = Fn extends (
  ...arg: infer V
) => infer T
  ? (...arg: [...V, A]) => T
  : never;

Solution by RanungPark #35515

// your answers
type AppendArgument<Fn, A> = Fn extends (...args: infer DefParams) => infer Result
    ? (...args: [...DefParams, A]) => Result
    : never;

Solution by Sathiyapramod #35462

type AppendArgument<Fn, A> = Fn extends (...args: infer R) => infer T ? (...args: [...R, A]) => T : never

Solution by HrOkiG2 #35327

type AppendArgument<Fn, A> = Fn extends (...args: infer R) => infer T ? (...args: [...R, A]) => T : never

Solution by HrOkiG2 #35301

type BuildParams<Params extends any[], A> = [...Params, x: A]

type AppendArgument<Fn extends (...args: any) => any, A> = 
  Fn extends (...args: infer Params) => infer Result 
    ? (...args: BuildParams<Params, A>) => Result 
    : never

Solution by eunsukimme #35140

// your answers
type AppendArgument<Fn, A> = Fn extends (...args: infer Args) => infer R ? (...args: [...Args, A]) => R : never;

Solution by LeeKangHyun #35011

Restrict Fn's type. Then infer Fn's args' type and return type.

// your answers
type AppendArgument<Fn extends (...args: any[]) => any, A> = Fn extends (...args: infer P) => infer R ? (...args: [...P, A]) => R : never;

Solution by dev-jaemin #35007

// 你的答案
type AppendArgument<Fn extends Function, A> = Fn extends (
  ...args: infer Args
) => infer R
  ? (...args: [...Args, A]) => R
  : never;

the case 3 will pass

Solution by shx123qwe #34954

I think we should use never instead of any

type AppendArgument<Fn extends (...args: never) => unknown, A> = Fn extends (...args: infer Args) => infer Return ? (...args: [...Args, A]) => Return : never

Solution by 2083335157 #34869

type AppendArgument<Fn, A> = Fn extends (...args: infer Args) => infer Return ? (...args:[...Args,A]) => Return : never

Solution by devshinthant #34576

// your answers
type AppendArgument<Fn, A> = Fn extends (...args: infer B)  => infer P? (...args: ([...B, ...[x: A]])) => P : never

Solution by gobielJonathan #34477

type AppendArgument<Fn extends Function, A> = Fn extends (...args: infer P) => infer R ? (...args: [...P, A]) => R : never

Solution by ouzexi #34005

type AppendArgument<Fn, U> = Fn extends (...args: infer P) => infer R ? (...args: [...P, U]) => R : never

Solution by Danny101201 #33852

type AppendArgument<Fn, A> = Fn extends (...arg: any[]) => any
  ? (...args: [...Parameters<Fn>, A]) => ReturnType<Fn>
  : never

Solution by laplace1009 #33785

type AppendArgument<F extends Function, U> = F extends (...args:infer P)=>infer R ? (...args:[...P, U])=> R: F

Solution by rookiewxy #33689

// 你的答案
type AppendArgument<F extends Function, U> = F extends (...args: infer R) => infer T
  ? (...args: [...R, U]) => T
  : never;

Solution by 2531800823 #33234

// your answers

type AppendArgument<Fn, A> =
  Fn extends (...args: infer Args) => infer R
    ? (...args: [...Args, A]) => R
    : never

Solution by KeithChou #33197

type AppendArgument<Fn extends Function, A extends unknown> = Fn extends (...args: infer Args) => infer Return
  ? (...args: [...Args, A]) => Return
  : never

Solution by Taneros #33171

// 你的答案
type AppendArgument<Fn extends (...args:any[])=>any, A> = Fn extends (...args:infer Params)=>infer R ? (...args:[...Params,A])=>R:never

Solution by walker-hzx #32939

type AppendArgument<Fn extends Function, A> = Fn extends (...args: infer T) => any ? (...args: [...T, A]) => ReturnType<Fn> : Fn

Solution by ZhipengYang0605 #32821

// 你的答案
type AppendArgument<Fn extends (...args: any) => any, A> = Fn extends (...args: infer R) => infer T ? (...args: [...R, A]) => T : never

Solution by geweidong #32765

type AppendArgument<Fn extends Function, A extends unknown> =
    Fn extends (...args: infer Args) => infer Return
        ? (...args: [...Args, x: A]) => Return
        : never

Solution by ZhulinskiiDanil #32678

type AppendArgument<Fn extends (...args: never[]) => unknown, A> = 
  Fn extends (...args: infer P) => infer R 
    ? (...args: [...P, A]) => R 
    : never

Solution by dev-hobin #32377

type AppendArgument<Fn extends (...args: any[]) => any, A> =
  Fn extends ((...args: infer P) => infer R)
  ? (...args: [...P, A]) => R
  : never

We infer the type of the arguments and append to it the new arg

Solution by joyanedel #32156

// your answers
type AppendArgument<Fn extends (...args:any[]) => any, A> = (...args: [...Parameters<Fn>, A]) => ReturnType<Fn>;

Solution by pea-sys #32039

type AppendArgument<Fn extends (...args: any[]) => any, A> = (...args: [...Parameters<Fn>, A]) => ReturnType<Fn>;

Solution by gasmg #31896

type AppendArgument<Fn extends Function, A> = Fn extends (...args: infer Arg) => infer R ? (...args: [...Arg, A]) => R : never;

Solution by kai-phan #31631

// your answers

type AppendArgument<F extends (...args: any[]) => any, T> = (
  ...args: [...Parameters<F>, x: T]
) => ReturnType<F>;

type AppendArgument<F, T> = F extends (...args: infer A) => infer R
  ? (...args: [...A, x: T]) => R
  : never;

Solution by AhmedRagabKamal #31510

type AppendArgument<Fn extends Function, A> = Fn extends (
  ...args: infer G
) => infer R
  ? (...arg1: [...G, A]) => R
  : never;

Solution by vipulpathak113 #31472

type AppendArgument<Fn extends Function, A> = Fn extends (...arg: infer Arg)=> infer R ? (...arg: [...Arg, A])=> R : Fn;

Solution by eward957 #31237