Skip to content

Commit 7463501

Browse files
committed
fix(partial): improve deep partial type to ensure compilation without errors when passing default values to create mock and create mock list
Partial deep source https://github.com/sindresorhus/type-fest/blob/master/source/partial-deep.d.ts
1 parent 42aae21 commit 7463501

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

src/create-mock-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { NoTransformerError } from './errors/no-transformer.error';
2-
import { DeepPartial } from './partial/deepPartial';
2+
import { PartialDeep } from './partial/partial';
33

44
export function createMockList<T extends object>(
55
// eslint-disable-next-line @typescript-eslint/no-unused-vars
66
quantity: number,
77
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8-
iterator?: (index: number) => DeepPartial<T>
8+
iterator?: (index: number) => PartialDeep<T>
99
): T[] {
1010
throw new Error(NoTransformerError);
1111
}

src/create-mock.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NoTransformerError } from './errors/no-transformer.error';
2-
import { DeepPartial } from './partial/deepPartial';
2+
import { PartialDeep } from './partial/partial';
33

44
// eslint-disable-next-line @typescript-eslint/no-unused-vars
5-
export function createMock<T extends object>(values?: DeepPartial<T>): T {
5+
export function createMock<T extends object>(values?: PartialDeep<T>): T {
66
throw new Error(NoTransformerError);
77
}

src/merge/merge.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { merge } from 'lodash-es';
2-
import { DeepPartial } from '../partial/deepPartial';
2+
import { PartialDeep } from '../partial/partial';
33

44
export class Merge {
5-
public static merge<T>(a: T, b: DeepPartial<T>): T {
5+
public static merge<T>(a: T, b: PartialDeep<T>): T {
66
return merge(a, b);
77
}
88

99
public static mergeIterator<T>(
1010
a: T,
11-
b: (index: number) => DeepPartial<T>,
11+
b: (index: number) => PartialDeep<T>,
1212
index: number
1313
): T {
1414
return merge(a, b(index));

src/partial/deepPartial.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/partial/partial.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
2+
3+
export type PartialDeep<T> = T extends Primitive
4+
? Partial<T>
5+
: T extends Map<infer KeyType, infer ValueType>
6+
? PartialMapDeep<KeyType, ValueType>
7+
: T extends Set<infer ItemType>
8+
? PartialSetDeep<ItemType>
9+
: T extends ReadonlyMap<infer KeyType, infer ValueType>
10+
? PartialReadonlyMapDeep<KeyType, ValueType>
11+
: T extends ReadonlySet<infer ItemType>
12+
? PartialReadonlySetDeep<ItemType> // eslint-disable-next-line @typescript-eslint/no-explicit-any
13+
: T extends (...args: any[]) => unknown
14+
? T | undefined
15+
: T extends object
16+
? PartialObjectDeep<T>
17+
: unknown;
18+
19+
/**
20+
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
21+
*/
22+
interface PartialMapDeep<KeyType, ValueType>
23+
extends Map<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
24+
25+
/**
26+
Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
27+
*/
28+
interface PartialSetDeep<T> extends Set<PartialDeep<T>> {}
29+
30+
/**
31+
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
32+
*/
33+
interface PartialReadonlyMapDeep<KeyType, ValueType>
34+
extends ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
35+
36+
/**
37+
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
38+
*/
39+
interface PartialReadonlySetDeep<T> extends ReadonlySet<PartialDeep<T>> {}
40+
41+
/**
42+
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
43+
*/
44+
type PartialObjectDeep<ObjectType extends object> = {
45+
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType]>;
46+
};

0 commit comments

Comments
 (0)