# withDefaults

## Quick example

```typescript
import { withDefaults } from "tsafe/lab/withDefaults";

function sum(params: { x: number; y: number; z: number }): number {
	const { x, y, z } = params;
	return x + y + z;
}

// sumWd is of type: (params: { y: number; z: number; })=> number
const sumWd = withDefaults(sum, { x: 10 });

console.log(sumWd({ y: 1, z: 2 })); // Prints "13" ( 10 + 1 + 2 )

console.log(sumWd({ y: 3, z: 4 })); // Prints "17" ( 10 + 3 + 4 )

console.log(
	sumWd({
		y: 3,
		z: 4,
		defaultsOverwrite: {
			x: [20],
		},
	})
); // Prints "27" ( 20 + 3 + 4 )
```

## In greater detail

If you have a function with a set of parameters wrapped in an object, and you wish to call this function multiple times with the same value for one or more of the parameters, `withDefaults` enables you to instantly generate a new function with these parameters already set so that you do not have to fill them in at every call.

Consider a function that takes two numbers as parameters and returns the sum of them.

```typescript
function sum(params: { x: number; y: number }) {
	const { x, y } = params;
	return x + y;
}
```

Suppose we want to set the value of `x` for example.

```typescript
const sumWd = withDefaults(sum, { x: 33 });
```

`sumWd` is a proxy to our original function with `x` set to `33`. `withDefaults` first argument is the original function, and the second is an object with the parameters of the original function as properties. Naturally, the properties are inferred by typescript as shown below.

![](https://2070518381-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_DPR4G1FRgI3DwBvcp%2Fuploads%2Fgit-blob-08be234297ed6f50654ab827098078cd9c6e971f%2FScreenshot%202021-05-13%20at%2017.36.40.png?alt=media)

Now we can call `sumWd` as many times as we want without having to set `x`. Its value will always be `33`.

```typescript
const result = sumWd({ y: 10 }); //43
```

The value of `result` will be `43`. Typescript infers the remaining value to be set:

![](https://2070518381-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M_DPR4G1FRgI3DwBvcp%2Fuploads%2Fgit-blob-df71aa63d0c974031c0bede5a126e8cd8d716cc5%2FScreenshot%202021-05-13%20at%2017.37.35.png?alt=media)

### Overwriting the injected value:

```typescript
const result = sumWd({
	y: 10,
	defaultsOverwrite: { x: [23] },
}); // 33
```

The type of `x` in `defaultsOverwrite` is `[number] | undefined` so that `undefined` cannot be assigned to `x` if that is not its type.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tsafe.dev/withdefaults.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
