Skip to content

Commit

Permalink
fix(partial): improve deep partial type to ensure compilation without…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
uittorio committed Aug 9, 2020
1 parent 42aae21 commit 7463501
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/create-mock-list.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { NoTransformerError } from './errors/no-transformer.error';
import { DeepPartial } from './partial/deepPartial';
import { PartialDeep } from './partial/partial';

export function createMockList<T extends object>(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
quantity: number,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
iterator?: (index: number) => DeepPartial<T>
iterator?: (index: number) => PartialDeep<T>
): T[] {
throw new Error(NoTransformerError);
}
4 changes: 2 additions & 2 deletions src/create-mock.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NoTransformerError } from './errors/no-transformer.error';
import { DeepPartial } from './partial/deepPartial';
import { PartialDeep } from './partial/partial';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function createMock<T extends object>(values?: DeepPartial<T>): T {
export function createMock<T extends object>(values?: PartialDeep<T>): T {
throw new Error(NoTransformerError);
}
6 changes: 3 additions & 3 deletions src/merge/merge.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { merge } from 'lodash-es';
import { DeepPartial } from '../partial/deepPartial';
import { PartialDeep } from '../partial/partial';

export class Merge {
public static merge<T>(a: T, b: DeepPartial<T>): T {
public static merge<T>(a: T, b: PartialDeep<T>): T {
return merge(a, b);
}

public static mergeIterator<T>(
a: T,
b: (index: number) => DeepPartial<T>,
b: (index: number) => PartialDeep<T>,
index: number
): T {
return merge(a, b(index));
Expand Down
7 changes: 0 additions & 7 deletions src/partial/deepPartial.ts

This file was deleted.

46 changes: 46 additions & 0 deletions src/partial/partial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
type Primitive = null | undefined | string | number | boolean | symbol | bigint;

export type PartialDeep<T> = T extends Primitive
? Partial<T>
: T extends Map<infer KeyType, infer ValueType>
? PartialMapDeep<KeyType, ValueType>
: T extends Set<infer ItemType>
? PartialSetDeep<ItemType>
: T extends ReadonlyMap<infer KeyType, infer ValueType>
? PartialReadonlyMapDeep<KeyType, ValueType>
: T extends ReadonlySet<infer ItemType>
? PartialReadonlySetDeep<ItemType> // eslint-disable-next-line @typescript-eslint/no-explicit-any
: T extends (...args: any[]) => unknown
? T | undefined
: T extends object
? PartialObjectDeep<T>
: unknown;

/**
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
*/
interface PartialMapDeep<KeyType, ValueType>
extends Map<PartialDeep<KeyType>, PartialDeep<ValueType>> {}

/**
Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
*/
interface PartialSetDeep<T> extends Set<PartialDeep<T>> {}

/**
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
*/
interface PartialReadonlyMapDeep<KeyType, ValueType>
extends ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>> {}

/**
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
*/
interface PartialReadonlySetDeep<T> extends ReadonlySet<PartialDeep<T>> {}

/**
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
*/
type PartialObjectDeep<ObjectType extends object> = {
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType]>;
};

0 comments on commit 7463501

Please sign in to comment.