Back to overview

Typescript typeof and ReturnType

Typescript

ReturnType takes a function type and produces its return type:

type Predicate = (x: unknown) => boolean;
type K = ReturnType<Predicate>;
// type K = boolean;

Don't forget typeof in below typing. type P = ReturnType<f>; is wrong.

function f() {
  return {x: 10, y: 3};
}
type P = ReturnType<typeof f>;

// type P = {
//     x: number;
//     y: number;
// }