Skip to content

Commit

Permalink
add declared type usage to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Raice Hannay committed Sep 10, 2023
1 parent 6ccef7b commit 0d4f2f1
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,30 @@ The value of the above `result` is:
```

All options have JSDoc descriptions [in its source](/index.ts#L82).


### When working with generic declared types/interfaces

There's currently a limitation with the inferred return type that `ts-deepmerge` offers, where it's
unable to take the order of the objects/properties into consideration due to the nature of accepting
an infinite number of objects to merge as args and what TypeScript currently offers to infer the types.
The primary use case for the inferred return type is for basic object primitives, to offer something
more useful as the return type, which does work for a lot of cases.

If you're working with generic declared types though, this can cause the inferred return type to not align
with what you may expect, as it currently detects every possible value and combines them as a union type.
When working with declared types, and you know what the final type will align to, simply use the `as` keyword
as shown in the example below:
```typescript
interface IObj {
a: string;
b: string;
}

const obj1: IObj = { a: "1", b: "2", };
const obj2: Partial<IObj> = { a: "1" };

const result = merge(obj1, obj2) as IObj;
```

More context can be found in [this issue](/~https://github.com/voodoocreation/ts-deepmerge/issues/30).

0 comments on commit 0d4f2f1

Please sign in to comment.