Syntax
ReturnType<typeof fn>
Parameters<typeof fn>Examples
ReturnType<T>
Extracting a function's return type without redefining it manually.
function createUser(name: string, age: number) {
return { id: Date.now(), name, age, createdAt: new Date() };
}
type User = ReturnType<typeof createUser>;
// User is inferred as: { id: number; name: string; age: number; createdAt: Date }
// No need to manually write out this shape - it stays in sync with the function automaticallyParameters<T>
Extracting a function's parameter types as a tuple.
function greet(name: string, greeting: string = "Hello") {
return `${greeting}, ${name}!`;
}
type GreetParams = Parameters<typeof greet>;
// GreetParams is: [name: string, greeting?: string]
function logCall(...args: Parameters<typeof greet>) {
console.log("Calling greet with:", args);
}Best practices
- Use ReturnType<typeof fn> to derive a type from a function's actual implementation, rather than manually duplicating and maintaining a matching interface
- Always use typeof when passing a function to these utilities (ReturnType<typeof myFunc>), since they operate on types, not runtime values
- Use Parameters<T> when writing a wrapper or decorator function that needs to accept the exact same arguments as another function
- These are especially valuable for third-party library functions whose exact return shape you do not want to manually re-type and keep in sync
Runtime & versions
Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
TypeScript Handbook