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 functionParameters<T>- Get parameter types as a tupleAwaited<T>- Unwrap Promise types
π¨ Open
and:
- Create
NewUserfromcreateUser's return type - Create
CreateUserParamsfromcreateUser's parameters - Create
FetchUserResultas the resolved value offetchUser(unwrap the Promise) - Create
ProcessDataArgsfromprocessData's parameters - Create
loggedCreateUserthat:- Accepts the same parameters as
createUser - Returns the same shape as
createUser - Can log the args, then call
createUser
- Accepts the same parameters as
- 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.