AI Hints
Home›TypeScript Tutorial›Types from Types
TypeScript · Lesson 67 of 120

ReturnType and Parameters

Types from functions.

⏱ 2 min read▶ Video at 51:24🎓 Free
Watch this lesson in the TypeScript courseWatch on YouTube ↗Full TypeScript course

Sometimes the only place a type is written is a function. ReturnType gives you what a function returns. Parameters gives you its parameters, as a tuple.

ReturnType and Parameters
From the video: ReturnType and Parameters
  • Types in functions
  • ReturnType
  • Parameters

Reusing a function's types

Make order returns an object we never named. ReturnType of typeof make order gives us that type anyway. Parameters gives the argument types, as a tuple. Run it.

app.tstypescript
function makeOrder(customer: string, total: number) {
  return { id: Math.floor(1000 + total), customer, total, status: "packing" as const };
}

type Order = ReturnType<typeof makeOrder>;
type OrderArgs = Parameters<typeof makeOrder>;   // [customer: string, total: number]

const args: OrderArgs = ["Priya", 65];
const order: Order = makeOrder(...args);
console.log(order);
Output
{ id: 1065, customer: 'Priya', total: 65, status: 'packing' }

Summary

ReturnType and Parameters pull types out of functions, so you don't repeat them. Next, conditional types.

Key takeaways
  • ReturnType<typeof f>
  • Parameters<typeof f>
  • No repeats
Next up: Conditional Types — if for types