Function Utility Types

Function Utilities
πŸ‘¨β€πŸ’Ό TypeScript provides utilities for working with function types. These are essential when wrapping or transforming functions.
function scoreGame(points: number, bonus: boolean) {
	return points + (bonus ? 10 : 0)
}

type ScoreResult = ReturnType<typeof scoreGame>
type ScoreArgs = Parameters<typeof scoreGame>
  • ReturnType<T> - Get the return type of a function
  • Parameters<T> - Get parameter types as a tuple
  • Awaited<T> - Unwrap Promise types
🐨 Open
index.ts
and:
  1. Use ReturnType to extract a function's return type
  2. Use Parameters to get a function's parameter types
  3. Use Awaited to unwrap Promise types
  4. Create a wrapper function that preserves types

Please set the playground first

Loading "Function Utility Types"
Loading "Function Utility Types"