TypeScript theorytheory 0/50 · 0%
Types · hard

39. ReturnType, Parameters & other type queries

Deriving types from existing functions instead of duplicating them.

`typeof` at the type level captures the type of an existing value, and utilities like `ReturnType<T>` and `Parameters<T>` extract pieces of a function type, keeping types in sync with implementation.

ts
function createWallet(seed: string, index: number) {
  return { address: `0x${seed}${index}`, index };
}

type Wallet = ReturnType<typeof createWallet>;
// { address: string; index: number }

type CreateWalletArgs = Parameters<typeof createWallet>;
// [seed: string, index: number]

function replay(args: CreateWalletArgs): Wallet {
  return createWallet(...args);
}

Deriving types this way means if `createWallet`'s signature changes, `Wallet` and `CreateWalletArgs` update automatically — no manually maintained duplicate interface can drift out of sync.

Check your understanding

  1. 1. What does `typeof createWallet` give you at the type level?

  2. 2. What does `ReturnType<typeof createWallet>` produce?

  3. 3. Why derive types with ReturnType/Parameters instead of duplicating an interface?