00191-medium-append-argument

Back

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

// Fn extends (...args: infer Args) => infer ReturnType:这里使用条件类型和 infer 关键字来推断原函数的参数类型(Args)和返回类型(ReturnType)。

// (...args: [...Args, A]) => ReturnType:创建了一个新的函数类型,它的参数是原始参数(Args)加上新追加的参数(A),并保持相同的返回类型(ReturnType)。

// never:如果传入的 Func 不是函数类型,结果类型将是 never。
type AppendArgument<Fn, A> = Fn extends (...args: infer Args) => infer ReturnType
  ? (...args: [...Args, A]) => ReturnType
  : never;

Solution by d1zzzzy #31135

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

Solution by MyeonghoonNam #31001

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

Solution by jaeilnet #30578

type AppendArgument<F extends (...args: any[]) => unknown, T> = F extends (...args: infer R) => infer Z ? (...args: [...R, T]) => Z : never

type Fn = (a: number, b: string) => number

type Result = AppendArgument<Fn, boolean>
// expected be (a: number, b: string, x: boolean) => number

Solution by rxMATTEO #30093