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
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {RequireExactlyOne} from './source/require-exactly-one';
export {RequireAllOrNone} from './source/require-all-or-none';
export {RemoveIndexSignature} from './source/remove-index-signature';
export {PartialDeep} from './source/partial-deep';
export {PartialOnUndefinedDeep} from './source/partial-on-undefined-deep';
export {ReadonlyDeep} from './source/readonly-deep';
export {LiteralUnion} from './source/literal-union';
export {Promisable} from './source/promisable';
Expand Down
47 changes: 47 additions & 0 deletions source/partial-on-undefined-deep.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type {BuiltIns} from './internal';
import type {Merge} from './merge';

/**
Create a type from another type with all keys and nested keys accepting undefined type set to optional.
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 :)


clemclx marked this conversation as resolved.
Show resolved Hide resolved
https://github.com/sindresorhus/type-fest/issues/399
@example
```
import type {PartialOnUndefinedDeep} from 'type-fest';
interface Settings {
optionA: string;
optionB: number | undefined;
subOption: {
subOptionA: boolean;
subOptionB: boolean | undefined;
}
};
const testSettings: PartialOnUndefinedDeep<Settings> = {
optionA: "foo",
// 👉 optionB is now optional and can be omited
subOption: {
subOptionA: true,
// 👉 subOptionB is now optional aswell and can be omited
clemclx marked this conversation as resolved.
Show resolved Hide resolved
},
};
```

@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
: never // Should not happen
: T;

/**
* Utility type to get the value type by key, recursively call PartialOnUndefinedDeep to treat subObjects
*/
clemclx marked this conversation as resolved.
Show resolved Hide resolved
type PartialOnUndefinedDeepValue<T> = T extends BuiltIns | ((...arguments: any[]) => unknown)
? T
: T extends Record<any, any> | undefined
? PartialOnUndefinedDeep<T>
: unknown;
46 changes: 46 additions & 0 deletions test-d/partial-on-undefined-deep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {expectType} from 'tsd';
import type {PartialOnUndefinedDeep} from '../index';

declare const foo: PartialOnUndefinedDeep<{
func: (() => void) | undefined;
obj: {objKey: 1} | undefined;
objDeep: {
subObj: string | undefined;
};
str: string | undefined;
union: 'test1' | 'test2' | undefined;
number: number | undefined;
bool: boolean | undefined;
date: Date | undefined;
regexp: RegExp | 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;
}>;

expectType<{
func?: typeof foo['func'];
obj?: typeof foo['obj'];
clemclx marked this conversation as resolved.
Show resolved Hide resolved
objDeep: {
subObj?: typeof foo['objDeep']['subObj'];
};
str?: typeof foo['str'];
union?: typeof foo['union'];
number?: typeof foo['number'];
bool?: typeof foo['bool'];
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'];
}>(foo);