Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added DeepPartial<T> mapped type #60

Merged
merged 1 commit into from
Jan 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ This gives you the power to prioritize our work and support project contributors
* [`DeepReadonly<T>`](#deepreadonlyt)
* [`DeepRequired<T>`](#deeprequiredt)
* [`DeepNonNullable<T>`](#deepnonnullablet)
* [`DeepPartial<T>`](#deeppartialt)

## Flow's Utility Types

Expand Down Expand Up @@ -554,6 +555,36 @@ type RequiredNestedProps = DeepNonNullable<NestedProps>;

---

### `DeepPartial<T>`

Partial that works for deeply nested structures

**Usage:**

```ts
import { DeepPartial } from 'utility-types';

type NestedProps = {
first: {
second: {
name: string;
};
};
};
type PartialNestedProps = DeepPartial<NestedProps>;
// Expect: {
// first?: {
// second?: {
// name?: string;
// };
// };
// }
```

[⇧ back to top](#mapped-types)

---

## Flow's Utility Types

### `$Keys<T>`
Expand Down
18 changes: 18 additions & 0 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ exports[`DeepNonNullable testType<DeepNonNullable<NestedProps>['first']['second'

exports[`DeepNonNullable testType<DeepNonNullable<NestedProps>['first']['second']['name']>() 1`] = `"string"`;

exports[`DeepPartial testType<ReturnType<NonNullable<typeof functionProp>>>() 1`] = `"string"`;

exports[`DeepPartial testType<typeof arrayItem.name>() 1`] = `"string | undefined"`;

exports[`DeepPartial testType<typeof arrayProp>() 1`] = `"_DeepPartialArray<{ name: string; }> | undefined"`;

exports[`DeepPartial testType<typeof functionProp>() 1`] = `"((value: number) => string) | undefined"`;

exports[`DeepPartial testType<typeof name>() 1`] = `"string | undefined"`;

exports[`DeepPartial testType<typeof nestedArrayPartial.first>() 1`] = `"_DeepPartialObject<{ second: { name: string; }[]; }> | undefined"`;

exports[`DeepPartial testType<typeof nestedFunctionPartial.first>() 1`] = `"_DeepPartialObject<{ second: (value: number) => string; }> | undefined"`;

exports[`DeepPartial testType<typeof partialNested.first>() 1`] = `"_DeepPartialObject<{ second: { name: string; }; }> | undefined"`;

exports[`DeepPartial testType<typeof second>() 1`] = `"_DeepPartialObject<{ name: string; }> | undefined"`;

exports[`DeepReadonly testType<DeepReadonly<NestedArrayProps>['first']>() 1`] = `"_DeepReadonlyObject<{ second: { name: string; }[]; }>"`;

exports[`DeepReadonly testType<DeepReadonly<NestedArrayProps>['first']['second']>() 1`] = `"_DeepReadonlyArray<{ name: string; }>"`;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {
DeepReadonly,
DeepRequired,
DeepNonNullable,
DeepPartial,
Diff,
FunctionKeys,
Intersection,
Expand Down
58 changes: 58 additions & 0 deletions src/mapped-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import {
DeepReadonly,
DeepRequired,
DeepNonNullable,
DeepPartial,
_DeepNonNullableArray,
_DeepNonNullableObject,
_DeepReadonlyArray,
_DeepReadonlyObject,
_DeepRequiredArray,
_DeepRequiredObject,
_DeepPartialObject,
_DeepPartialArray,
} from './mapped-types';

/**
Expand Down Expand Up @@ -279,3 +282,58 @@ it('DeepNonNullable', () => {
ReturnType<DeepNonNullable<NestedFunctionProps>['first']['second']>
>();
});

// @dts-jest:group DeepPartial
it('DeepPartial', () => {
type NestedProps = {
first: {
second: {
name: string;
};
};
};
const partialNested: DeepPartial<NestedProps> = {} as any;
// @dts-jest:pass:snap -> _DeepPartialObject<{ second: { name: string; }; }> | undefined
testType<typeof partialNested.first>();

const second = partialNested.first!.second;
// @dts-jest:pass:snap -> _DeepPartialObject<{ name: string; }> | undefined
testType<typeof second>();

const name = second!.name;
// @dts-jest:pass:snap -> string | undefined
testType<typeof name>();

type NestedArrayProps = {
first: {
second: Array<{ name: string }>;
};
};

const nestedArrayPartial: DeepPartial<NestedArrayProps> = {};
// @dts-jest:pass:snap -> _DeepPartialObject<{ second: { name: string; }[]; }> | undefined
testType<typeof nestedArrayPartial.first>();

const arrayProp = nestedArrayPartial.first!.second;
// @dts-jest:pass:snap -> _DeepPartialArray<{ name: string; }> | undefined
testType<typeof arrayProp>();

const arrayItem = arrayProp![0];
// @dts-jest:pass:snap -> string | undefined
testType<typeof arrayItem.name>();

type NestedFunctionProps = {
first: {
second: (value: number) => string;
};
};
const nestedFunctionPartial: DeepPartial<NestedFunctionProps> = {};
// @dts-jest:pass:snap -> _DeepPartialObject<{ second: (value: number) => string; }> | undefined
testType<typeof nestedFunctionPartial.first>();

const functionProp = nestedFunctionPartial.first!.second;
// @dts-jest:pass:snap -> ((value: number) => string) | undefined
testType<typeof functionProp>();
// @dts-jest:pass:snap -> string
testType<ReturnType<NonNullable<typeof functionProp>>>();
});
58 changes: 58 additions & 0 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import {
DeepReadonly,
DeepRequired,
DeepNonNullable,
DeepPartial,
_DeepNonNullableArray,
_DeepNonNullableObject,
_DeepReadonlyArray,
_DeepReadonlyObject,
_DeepRequiredArray,
_DeepRequiredObject,
_DeepPartialObject,
_DeepPartialArray,
} from './mapped-types';

/**
Expand Down Expand Up @@ -279,3 +282,58 @@ it('DeepNonNullable', () => {
ReturnType<DeepNonNullable<NestedFunctionProps>['first']['second']>
>();
});

// @dts-jest:group DeepPartial
it('DeepPartial', () => {
type NestedProps = {
first: {
second: {
name: string;
};
};
};
const partialNested: DeepPartial<NestedProps> = {} as any;
// @dts-jest:pass:snap
testType<typeof partialNested.first>();

const second = partialNested.first!.second;
// @dts-jest:pass:snap
testType<typeof second>();

const name = second!.name;
// @dts-jest:pass:snap
testType<typeof name>();

type NestedArrayProps = {
first: {
second: Array<{ name: string }>;
};
};

const nestedArrayPartial: DeepPartial<NestedArrayProps> = {};
// @dts-jest:pass:snap
testType<typeof nestedArrayPartial.first>();

const arrayProp = nestedArrayPartial.first!.second;
// @dts-jest:pass:snap
testType<typeof arrayProp>();

const arrayItem = arrayProp![0];
// @dts-jest:pass:snap
testType<typeof arrayItem.name>();

type NestedFunctionProps = {
first: {
second: (value: number) => string;
};
};
const nestedFunctionPartial: DeepPartial<NestedFunctionProps> = {};
// @dts-jest:pass:snap
testType<typeof nestedFunctionPartial.first>();

const functionProp = nestedFunctionPartial.first!.second;
// @dts-jest:pass:snap
testType<typeof functionProp>();
// @dts-jest:pass:snap
testType<ReturnType<NonNullable<typeof functionProp>>>();
});
17 changes: 17 additions & 0 deletions src/mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,20 @@ export interface _DeepNonNullableArray<T>
export type _DeepNonNullableObject<T> = {
[P in keyof T]-?: DeepNonNullable<NonNullable<T[P]>>
};

/**
* DeepPartial
* @desc Partial that works for deeply nested structure
*/
export type DeepPartial<T> =
T extends Function ? T :
T extends Array<infer U> ? _DeepPartialArray<U> :
T extends object ? _DeepPartialObject<T> :
T | undefined;
/** @private */
// tslint:disable-next-line:class-name
export interface _DeepPartialArray<T> extends Array<DeepPartial<T>> { }
/** @private */
export type _DeepPartialObject<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};