Disclosure

Most of the problems under my TypeHero Challenges folder were either obtained from typehero.dev or from type-challenges repo. Purpose of these articles are just to document my approaches for my easy reference. Please visit the respective links for more info.

Link to original

Problem Description

For given function type Fn, 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 Fn as the first argument, A as the second, and will produce function type G which will be the same as Fn but with appended argument A as a last one.

For example:

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

Solutions

Approach 1

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