> For the complete documentation index, see [llms.txt](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kinesis-school-of-programming.gitbook.io/nestjs-unleashed/core-module-backend-development-with-nestjs/remaining-domain/logic/entity-validation/function-overload.md).

# Function overload

One implementation for many signatures.

Another approach would be to simply receive the <mark style="color:blue;">`categoriesIds`</mark> in the DTO.

```typescript
@ArrayNotEmpty()
@ArrayUnique()
@IsCardinal({ each: true })
readonly categoriesIds: number[];
```

After that, we could transform them to the objects mentioned in the previous solution. For better encapsulation, in the file <mark style="color:purple;">common</mark>/<mark style="color:purple;">util</mark>/<mark style="color:purple;">id.util</mark>, create the function <mark style="color:blue;">`wrapId()`</mark>. We'll use a **function overload** to have just a single implementation, preventing the need to write two separate functions depending on the parameter (one or many ids). The tooltip adapts depending on the input provided.

```typescript
export function wrapId(id: number): IdDto;
export function wrapId(ids: number[]): IdDto[];
export function wrapId(idOrIds: number | number[]) {
  if (Array.isArray(idOrIds)) {
    const ids = idOrIds;
    return ids.map((id) => ({ id }));
  }

  const id = idOrIds;
  return { id };
}
```

{% hint style="info" %}
Notice the following:

* The last signature is the **implementation** one, not available when calling
* The **return type** needs to be explicit when overloading
* The function is used in its **regular form** (not arrow) to allow for the overload
  {% endhint %}

What remains to be done is to, in the <mark style="color:blue;">`ProductsService`</mark>, wrap the ids and use them.

```typescript
create(createProductDto: CreateProductDto) {
  const { categoriesIds } = createProductDto;
  const categories = wrapId(categoriesIds);

  const product = this.productsRepository.create({
    ...createProductDto,
    categories,
  });
  return this.productsRepository.save(product);
}
```

<mark style="color:green;">**Commit**</mark> - Function overload for wrapping ids
