...

/

The `infer` Keyword

The `infer` Keyword

The lesson goes through some of the remaining built-in conditional types and introduces the `infer` keyword.

We'll cover the following...

Parameters

The Parameters type takes a function type and returns a tuple type representing types of all parameters of the function. Sounds magical, doesn’t it?

Press + to interact
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
const sayHello = (name: string, age: number) => `Hello ${name}, your age is ${age}`;
type SayHelloParams = Parameters<typeof sayHello>; // [string, number]

Let’s break down this definition. First, the Parameters type has a type argument constraint ...