## DEV Community is a community of 558,324 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Advanced TypeScript Exercises - Question 4 Maciej Sikora Updated on ・1 min read

For given function type `F`, and any type `A` (any in this context means we don't restrict the type, and I don't have in mind `any` type 😉) create a generic type which will take `F` as first argument, `A` as second and will produce function type `G` which will be the same as `F` but with appended argument `A` as a first one.

``````// lets say we have some function type
type SomeF = (a: number, b: string) => number
// and we have our utility type
type AppendArgument<F, A> = ... here your code 💪

type FinalF = AppendArgument<SomeF, boolean>
// FinalF should be (x: boolean, a: number, b: string) => number
``````

## Discussion  Rahul Kashyap
``````// lets say we have some function type
type SomeF = (a: number, b: string) => number

// and we have our utility type
type AppendArgument<F extends (...args: any[]) => any, A> = (x: A, ...args: Parameters<F>) => ReturnType<F>

type FinalF = AppendArgument<SomeF, boolean>
// FinalF should be (x: boolean, a: number, b: string) => number
`````` Jasper De Sutter
``````// lets say we have some function type
type SomeF = (a: number, b: string) => number
// and we have our utility type
type AppendArgument<F, A> = F extends (...args: infer Args) => infer R ? (x: A, ...args: Args) => R : never

type FinalF = AppendArgument<SomeF, boolean>
// FinalF should be (x: boolean, a: number, b: string) => number
`````` John Ralph Umandal
``````// lets say we have some function type
type SomeF = (a: number, b: string) => number;
// and we have our utility type
type AppendArgument<F, A> =
F extends (...[a, b, ...oth]: [infer ArgA, infer ArgB, infer ArgOth]) => infer FReturn
? (x: A, ...[a, b, ...oth]: [ArgA, ArgB, ArgOth]) => FReturn
: unknown;

type FinalF = AppendArgument<SomeF, boolean>
// FinalF should be (x: boolean, a: number, b: string) => number
``````

I wasn't able to figure out myself how to dynamically take the pair `arg:type` out from a generic type function :(

UPDATE: palmface

``````// lets say we have some function type
type SomeF = (a: number, b: string) => number;
// and we have our utility type
type AppendArgument<F, A> =
F extends (...args: infer Args) => infer FReturn
? (x: A, ...args: Args) => FReturn
: unknown;

type FinalF = AppendArgument<SomeF, boolean>
// FinalF should be (x: boolean, a: number, b: string) => number
``````