EasyUtility Types📖 Theory Question

What are ReturnType, Parameters, and InstanceType utility types?

💡

Hint

Infer the output type of a function, the input tuple, or the instance type of a class constructor

Full Answer

These utility types extract type information from function and class signatures.

ReturnType<T> — extracts the return type of a function type:

function fetchUser(id: number): Promise<User> { /* ... */ }

type FetchResult = ReturnType<typeof fetchUser>; // Promise<User>
type User2 = Awaited<ReturnType<typeof fetchUser>>; // User (unwrapped)

// Useful when the return type is complex and you don't want to duplicate it
const config = { theme: 'dark', lang: 'en', debug: false };
type Config = ReturnType<typeof createConfig>; // mirrors the function output

Parameters<T> — extracts the parameter types as a tuple:

function createUser(name: string, age: number, admin?: boolean): User { /* ... */ }

type CreateUserParams = Parameters<typeof createUser>;
// [name: string, age: number, admin?: boolean]

// Useful for wrapping or forwarding function calls
function withLogging<T extends (...args: any[]) => any>(
  fn: T,
  ...args: Parameters<T>
): ReturnType<T> {
  console.log('Calling with', args);
  return fn(...args);
}

InstanceType<T> — extracts the instance type from a constructor:

class Connection {
  host: string;
  connect(): void { /* ... */ }
}

type ConnectionInstance = InstanceType<typeof Connection>; // Connection

// Useful in factory patterns where you work with constructors
function create<T extends new (...args: any[]) => any>(
  Ctor: T, ...args: ConstructorParameters<T>
): InstanceType<T> {
  return new Ctor(...args);
}
💡 These three types work with typeof to extract shapes from existing values — no need to manually define return types or parameter interfaces for functions you already have.

More Utility Types Questions

EasyWhat are Partial, Required, and Readonly utility types?→EasyWhat are Pick and Omit utility types and when should you use them?→EasyWhat is the Record utility type and when is it useful?→EasyWhat are Exclude and Extract utility types?→

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint