Typescript: Get the type of the last parameter of a function type
As of TypeScript 4.0 with variadic tuple support, it's also possible to use the following to get the last type in a tuple type:
type LastType<T extends [unknown, ...Array<unknown>]> = T extends [...infer A, infer L] ? L : never;
Here is yet a another solution which is a slight modification of jcalz answer to make it more generic using some recursive conditional types:
type Head<T extends any[]> = T extends [any, ...any[]] ? T[0] : never;
type Tail<T extends any[]> =
((...t: T) => any) extends ((_: any, ...tail: infer U) => any)
? U
: [];
type HasTail<T extends any[]> = T extends ([] | [any]) ? false : true;
type Last<T extends any[]> = {
0: Last<Tail<T>>
1: Head<T>
}[
HasTail<T> extends true ? 0 : 1
];
type LastType = Last<Parameters<somefntype>>; // boolean
It might be interesting to consider replacing any
with unknown
as well.
Update: TypeScript 4.0 introduced variadic tuple types, which means that Last
can now be implemented simply as
type Last<T extends any[]> = T extends [...infer I, infer L] ? L : never;
And the rest works as normal:
type LastParameter<F extends (...args: any)=>any> = Last<Parameters<F>>;
type somefntype = (a: number, b: string, c: boolean) => void
type lastparamtype = LastParameter<somefntype> // boolean
Playground link to code
TypeScript 3.0 introduced tuple types in rest and spread expressions, specifically to allow programmatic conversion between the types of function parameter lists and tuples. There's a type alias called Parameters<F>
defined in the standard library that returns the parameter list of a function as a tuple.
It's annoying but possible to get the last element of a tuple type with a fixed but arbitrary length:
// Tail<T> returns a tuple with the first element removed
// so Tail<[1, 2, 3]> is [2, 3]
// (works by using rest tuples)
type Tail<T extends any[]> =
((...t: T)=>void) extends ((h: any, ...r: infer R)=>void) ? R : never;
// Last<T> returns the last element of the tuple
// (works by finding the one property key in T which is not in Tail<T>)
type Last<T extends any[]> = T[Exclude<keyof T, keyof Tail<T>>];
Putting those together you get
type LastParameter<F extends (...args: any)=>any> = Last<Parameters<F>>;
And we can test it:
type somefntype = (a: number, b: string, c: boolean) => void
type lastparamtype = LastParameter<somefntype> // boolean
Looks good to me. Link to code