Skip to content

Commit 990c46d

Browse files
committed
We were missing boolean and Date support. That's been added now.
1 parent f6cd9e8 commit 990c46d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/conditionalTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type OnlyOptionalValues<T> = SetDifference<OptionalValues<T>, truthyNonCurlies>;
1010
export type NestedPartialWarningStr = 'mergePartially.deep does not allow a seed object to have values on it that are optional objects. Please use mergePartially.shallow instead. See https://github.com/dgreene1/merge-partially/blob/master/whyShallowInstead.md for more information.';
1111

1212
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13-
type truthyNonCurlies = number | Date | string | symbol | Set<any> | any[] | (() => any) | null | undefined;
13+
type truthyNonCurlies = number | boolean | Date | string | symbol | Set<any> | any[] | (() => any) | null | undefined;
1414

1515
export type NoNestedOptionalObjectsDeep<T> = OnlyOptionalValues<T> extends never
1616
? NestedPartialProblemPreventer<T>

src/index.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,39 @@ describe('mergePartially', () => {
159159
expect(seed.optionalProp).toEqual(undefined);
160160
});
161161

162+
it('should override an optional date if provided, and leave un-overriden dates as-is', () => {
163+
interface IObjWithDate {
164+
aRequiredString: string;
165+
aRequiredDate: Date;
166+
aDate?: Date;
167+
}
168+
const seed: IObjWithDate = {
169+
aRequiredString: 'hello',
170+
aRequiredDate: new Date('2000-01-01'),
171+
};
172+
173+
// Act
174+
const result = mergePartially.deep(seed, {
175+
aRequiredString: 'hello',
176+
aDate: new Date('2020-09-17'),
177+
});
178+
179+
// Assert
180+
// First check that the dates haven't been stringified
181+
expect(result.aDate).toBeInstanceOf(Date);
182+
expect(result.aRequiredDate).toBeInstanceOf(Date);
183+
expect(result).toEqual({
184+
aDate: new Date('2020-09-17'),
185+
aRequiredDate: new Date('2000-01-01'),
186+
aRequiredString: 'hello',
187+
});
188+
// Prove that mergePartially is a pure function
189+
expect(seed).toEqual({
190+
aRequiredString: 'hello',
191+
aRequiredDate: new Date('2000-01-01'),
192+
});
193+
});
194+
162195
it('supports nested objects (automatically with mergePartially.deep)', () => {
163196
interface ITestCase {
164197
a: string;
@@ -169,6 +202,7 @@ describe('mergePartially', () => {
169202
b3a: string;
170203
b3b: string;
171204
b3c?: string;
205+
b3d?: boolean;
172206
};
173207
};
174208
c: string;
@@ -194,6 +228,7 @@ describe('mergePartially', () => {
194228
b3: {
195229
b3a: undefined,
196230
b3b: 'new value for b3b',
231+
b3d: false,
197232
},
198233
},
199234
c: 'new c',

0 commit comments

Comments
 (0)