Skip to content

Commit 3d016dd

Browse files
committed
better type errors and a fix for #100
1 parent c4ec881 commit 3d016dd

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
- [Design Goals](#design-goals)
1010
- [Why would you want to use this:](#why-would-you-want-to-use-this)
11-
* [First, let's try to write the flexible factory function without mergePartially](#first-lets-try-to-write-the-flexible-factory-function-without-mergepartially)
12-
* [Now let's refactor using mergePartially](#now-lets-refactor-using-mergepartially)
11+
- [First, let's try to write the flexible factory function without mergePartially](#first-lets-try-to-write-the-flexible-factory-function-without-mergepartially)
12+
- [Now let's refactor using mergePartially](#now-lets-refactor-using-mergepartially)
1313
- [Examples](#examples)
1414
- [F.A.Q. / Troubleshooting](#faq--troubleshooting)
15-
* [Why wouldn't I just use Object.assign or the spread operator?](#why-wouldnt-i-just-use-objectassign-or-the-spread-operator)
16-
* [I see lots of TypeScript stuff. Can I use this in JavaScript too?](#i-see-lots-of-typescript-stuff-can-i-use-this-in-javascript-too)
17-
* [What's the difference between .deep and .shallow?](#whats-the-difference-between-deep-and-shallow)
18-
* [Why is `.shallow` even necessary?](#why-is-shallow-even-necessary)
19-
* [Why is my return type some strange error string?](#why-is-my-return-type-some-strange-error-string)
15+
- [Why wouldn't I just use Object.assign or the spread operator?](#why-wouldnt-i-just-use-objectassign-or-the-spread-operator)
16+
- [I see lots of TypeScript stuff. Can I use this in JavaScript too?](#i-see-lots-of-typescript-stuff-can-i-use-this-in-javascript-too)
17+
- [What's the difference between .deep and .shallow?](#whats-the-difference-between-deep-and-shallow)
18+
- [Why is `.shallow` even necessary?](#why-is-shallow-even-necessary)
19+
- [Why is my return type some strange error string?](#why-is-my-return-type-some-strange-error-string)
2020
- [Contributions](#contributions)
2121

2222
<!-- tocstop -->
@@ -157,6 +157,10 @@ const shallowResult = mergePartially.shallow(seed, {
157157

158158
There are some data types that are "less-compatible" with the library and therefore require a workaround ([click here for the description](https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md)). It should be rare that you need to use `.shallow`, but you might prefer `.shallow` over `.deep` anyway for explicitness.
159159

160+
### Why am I getting some error about `never`?
161+
162+
If you're seeing a Typescript error similar to `"Type 'number' is not assignable to type 'never'"` then you it's likely a case where you need to inform the function what the type is for your `seed` parameter. See [this thread](https://github.com/dgreene1/merge-partially/issues/100#issuecomment-694866025) for a detailed answer and a fun description in how TypeScript works.
163+
160164
### Why is my return type some strange error string?
161165

162166
In order to meet the design goals (see above), mergePartially proactively prevents certain data combinations. See this link for more information on the soluton: [https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md](https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md)

src/index.spec.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,23 @@ describe('mergePartially', () => {
3030
});
3131

3232
it('should overwrite a boolean even a falsy (i.e. false)', () => {
33-
const original = {
34-
a: 'a',
35-
b: true,
36-
};
33+
interface IHaveABoolAndStr {
34+
a: string;
35+
b: boolean;
36+
}
3737

38-
const result = mergePartially.deep(original, {
39-
b: false,
40-
});
38+
// Notice that.......... you can explicitly type the seed by passing in a <type> parameter
39+
const result = mergePartially.deep<IHaveABoolAndStr>(
40+
{
41+
a: 'a',
42+
b: true,
43+
},
44+
{
45+
b: false,
46+
}
47+
);
4148

4249
expect(result.b).toEqual(false);
43-
// Prove that mergePartially is a pure function
44-
expect(original.b).toEqual(true);
4550
});
4651

4752
it('should not replace missing properties, but should replace present properties with falsy values', () => {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ const mergePartiallyShallow = <T1 extends object>(seed: T1, override: Partial<T1
122122
* @param seed the object that is used establish the start of what you want the result to look like. This is the object that will be overriden before a result is produced
123123
* @param override the data that will be used when replacing the seed's key/values
124124
*/
125-
const mergePartiallyDeep = <T1 extends object, T2 extends NestedPartial<T1> | undefined = undefined>(
125+
const mergePartiallyDeep = <T1 extends object, T2 extends NestedPartial<T1> = NestedPartial<T1>>(
126126
seed: T1,
127-
override: T2
127+
override: T2 | undefined
128128
): NoNestedOptionalObjectsDeep<T1> => {
129129
return mergePartiallyShallow(seed, override) as NoNestedOptionalObjectsDeep<T1>;
130130
};

0 commit comments

Comments
 (0)