# Parameters & ReturnType

> Extract types directly from function signatures.

Subject: TypeScript | Lesson 40 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/parameters-and-returntype
Previous: [Exclude & Extract](https://codewithanimation.in/typescript/exclude-and-extract.md) | Next: [The Awaited Type](https://codewithanimation.in/typescript/awaited-type.md)

## Reverse Engineering Functions

`Parameters<Type>` extracts the arguments of a function type and returns them as a Tuple.

`ReturnType<Type>` extracts the value that the function returns.

## Third-Party Libraries

Often, you'll use a third-party library that exports a function, but forgets to export the types for its arguments or its result.

Instead of guessing or manually copying the types, you can use these utilities to extract them directly from the function's signature. This guarantees your code stays in sync with the library.

## Syntax

```typescript
function createUser(name: string, age: number): { id: string } {
  return { id: "123" };
}

// Extract the return type
type UserResponse = ReturnType<typeof createUser>; // { id: string }

// Extract the arguments as a Tuple
type UserArgs = Parameters<typeof createUser>; // [name: string, age: number]

// Access just the first argument using indexed access
type NameArg = Parameters<typeof createUser>[0]; // string
```

## Common mistake: Tuple Headaches

`Parameters` always returns a Tuple (an array type with fixed lengths and types at specific indices).

If you try to pass `Parameters<typeof foo>` to another function, you usually have to use the spread operator `...args: Parameters<typeof foo>` to unpack the tuple back into normal arguments.

## Tip: The Power of infer

These utilities are built using conditional types and the `infer` keyword.

`type ReturnType<T> = T extends (...args: any) => infer R ? R : any;`

TypeScript looks at the function, "infers" what `R` is, and returns it!

## Try it: Try it

Hover over the utilities to see how they use the `infer` keyword to reach inside the function signature and pull out the specific types.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/parameters-and-returntype

## Check yourself

### Question 1

If `function greet(name: string): boolean`, what is `Parameters<typeof greet>`?

- A. string
- B. [name: string]
- C. { name: string }

Correct answer: B

Parameters ALWAYS returns a Tuple (an array of types) representing the function's arguments in order.

- Why not A: Parameters returns a tuple of ALL arguments.
- Why not C: It returns a tuple (array), not an object.

## Remember this

- `ReturnType<typeof fn>` extracts what a function returns.
- `Parameters<typeof fn>` extracts the arguments into a Tuple.
- Use `[0]` to get the first argument from the `Parameters` tuple.
- They are powered by the `infer` keyword in conditional types.
