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. Create NewUser from createUser's return type
  2. Create CreateUserParams from createUser's parameters
  3. Create FetchUserResult as the resolved value of fetchUser (unwrap the Promise)
  4. Create ProcessDataArgs from processData's parameters
  5. Create loggedCreateUser that:
    • Accepts the same parameters as createUser
    • Returns the same shape as createUser
    • Can log the args, then call createUser
  6. Export loggedCreateUser
Completion check: loggedCreateUser('Alice', 'alice@example.com', 30) returns an object with id, name: 'Alice', email: 'alice@example.com', age: 30, and createdAt.

Please set the playground first

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