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

Add PartialOnUndefinedDeep type #426

Merged
merged 17 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 27 additions & 6 deletions source/partial-on-undefined-deep.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import type {BuiltIns} from './internal';
import type {Merge} from './merge';

/**
@see PartialOnUndefinedDeep
*/
export interface PartialOnUndefinedDeepOptions {
clemclx marked this conversation as resolved.
Show resolved Hide resolved
/**
Whether to affect the individual elements of arrays and tuples
clemclx marked this conversation as resolved.
Show resolved Hide resolved

@default false
*/
readonly recurseIntoArrays?: boolean;
}

/**
Create a deep version of another type where all keys accepting `undefined` type are set to optional.

This utility type is recursive, transforming at any level deep, and recurses into arrays too.
This utility type is recursive, transforming at any level deep.
By default, this type does not affects array and tuple types unless you explicitly pass `{recurseIntoArrays: true}` as the second type argument.
clemclx marked this conversation as resolved.
Show resolved Hide resolved

Use-cases:
- Make all properties of a type that can be undefined optional to not have to specify keys with undefined value
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you come up with some more use-cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure to have more use-cases that would be pertinent/understandable for users, let me know if something seems pertinent for you :)

Expand Down Expand Up @@ -35,17 +48,25 @@ const testSettings: PartialOnUndefinedDeep<Settings> = {

@category Object
*/
export type PartialOnUndefinedDeep<T> = T extends Record<any, any> | undefined
? {[KeyType in keyof T as undefined extends T[KeyType] ? KeyType : never]?: PartialOnUndefinedDeepValue<T[KeyType]>} extends infer U // Make a partial type with all value types accepting undefined (and set them optional)
? Merge<{[KeyType in keyof T as KeyType extends keyof U ? never : KeyType]: PartialOnUndefinedDeepValue<T[KeyType]>}, U> // Join all remaining keys not treated in U
export type PartialOnUndefinedDeep<T, Options extends PartialOnUndefinedDeepOptions = {}> = T extends Record<any, any> | undefined
? {[KeyType in keyof T as undefined extends T[KeyType] ? KeyType : never]?: PartialOnUndefinedDeepValue<T[KeyType], Options>} extends infer U // Make a partial type with all value types accepting undefined (and set them optional)
? Merge<{[KeyType in keyof T as KeyType extends keyof U ? never : KeyType]: PartialOnUndefinedDeepValue<T[KeyType], Options>}, U> // Join all remaining keys not treated in U
: never // Should not happen
: T;

/**
Utility type to get the value type by key and recursively call `PartialOnUndefinedDeep` to transform sub-objects.
*/
type PartialOnUndefinedDeepValue<T> = T extends BuiltIns | ((...arguments: any[]) => unknown)
type PartialOnUndefinedDeepValue<T, Options extends PartialOnUndefinedDeepOptions> = T extends BuiltIns | ((...arguments: any[]) => unknown)
? T
: T extends ReadonlyArray<infer U> // Test if type is array or tuple
? Options['recurseIntoArrays'] extends true // Check if option is activated
? U[] extends T // Check if array not tuple
? readonly U[] extends T
? ReadonlyArray<PartialOnUndefinedDeep<U, Options>> // Readonly array treatment
: Array<PartialOnUndefinedDeep<U, Options>> // Mutable array treatment
: PartialOnUndefinedDeep<{[Key in keyof T]: PartialOnUndefinedDeep<T[Key], Options>}, Options> // Tuple treatment
: T
: T extends Record<any, any> | undefined
? PartialOnUndefinedDeep<T>
? PartialOnUndefinedDeep<T, Options>
: unknown;
82 changes: 57 additions & 25 deletions test-d/partial-on-undefined-deep.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expectType} from 'tsd';
import {expectAssignable} from 'tsd';
import type {PartialOnUndefinedDeep} from '../index';

declare const foo: PartialOnUndefinedDeep<{
type TestingType = {
function: (() => void) | undefined;
object: {objectKey: 1} | undefined;
objectDeep: {
Expand All @@ -13,34 +13,66 @@ declare const foo: PartialOnUndefinedDeep<{
boolean: boolean | undefined;
date: Date | undefined;
regexp: RegExp | undefined;
symbol: Symbol | undefined;
symbol: symbol | undefined;
null: null | undefined;
record: Record<string, any> | undefined;
map: Map<string, string> | undefined;
set: Set<string> | undefined;
array: any[] | undefined;
tuple: ['test1'] | undefined;
readonly: readonly string[] | undefined;
}>;
array1: any[] | undefined;
array2: Array<{propertyA: string; propertyB: number | undefined}> | undefined;
readonly1: readonly any[] | undefined;
readonly2: ReadonlyArray<{propertyA: string; propertyB: number | undefined}> | undefined;
tuple: ['test1', {propertyA: string; propertyB: number | undefined}] | undefined;
};

expectType<{
function?: typeof foo['function'];
object?: typeof foo['object'];
// Default behavior, without recursion into arrays/tuples
declare const foo: PartialOnUndefinedDeep<TestingType>;
expectAssignable<{
function?: TestingType['function'];
object?: TestingType['object'];
objectDeep: {
subObject?: typeof foo['objectDeep']['subObject'];
subObject?: TestingType['objectDeep']['subObject'];
};
string?: typeof foo['string'];
union?: typeof foo['union'];
number?: typeof foo['number'];
boolean?: typeof foo['boolean'];
date?: typeof foo['date'];
regexp?: typeof foo['regexp'];
symbol?: typeof foo['symbol'];
null?: typeof foo['null'];
record?: typeof foo['record'];
map?: typeof foo['map'];
set?: typeof foo['set'];
array?: typeof foo['array'];
tuple?: typeof foo['tuple'];
readonly?: typeof foo['readonly'];
string?: TestingType['string'];
union?: TestingType['union'];
number?: TestingType['number'];
boolean?: TestingType['boolean'];
date?: TestingType['date'];
regexp?: TestingType['regexp'];
symbol?: TestingType['symbol'];
null?: TestingType['null'];
record?: TestingType['record'];
map?: TestingType['map'];
set?: TestingType['set'];
array1?: TestingType['array1'];
array2?: TestingType['array2'];
readonly1?: TestingType['readonly1'];
readonly2?: TestingType['readonly2'];
tuple?: TestingType['tuple'];
}>(foo);

// With recursion into arrays/tuples activated
declare const bar: PartialOnUndefinedDeep<TestingType, {recurseIntoArrays: true}>;
expectAssignable<{
function?: TestingType['function'];
object?: TestingType['object'];
objectDeep: {
subObject?: TestingType['objectDeep']['subObject'];
};
string?: TestingType['string'];
union?: TestingType['union'];
number?: TestingType['number'];
boolean?: TestingType['boolean'];
date?: TestingType['date'];
regexp?: TestingType['regexp'];
symbol?: TestingType['symbol'];
null?: TestingType['null'];
record?: TestingType['record'];
map?: TestingType['map'];
set?: TestingType['set'];
array1?: TestingType['array1'];
array2?: Array<{propertyA: string; propertyB?: number | undefined}> | undefined;
readonly1?: TestingType['readonly1'];
readonly2?: ReadonlyArray<{propertyA: string; propertyB?: number | undefined}> | undefined;
tuple?: ['test1', {propertyA: string; propertyB?: number | undefined}] | undefined;
}>(bar);