-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com> Co-authored-by: Dimitri Benin <BendingBender@users.noreply.github.com>
- Loading branch information
1 parent
b29c31a
commit 80465bc
Showing
4 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import {Primitive} from './basic'; | ||
|
||
/** | ||
Create a type from another type with all keys and nested keys set to optional. | ||
Use-cases: | ||
- Merging a default settings/config object with another object, the second object would be a deep partial of the default object. | ||
- Mocking and testing complex entities, where populating an entire object with its properties would be redundant in terms of the mock or test. | ||
@example | ||
``` | ||
import {PartialDeep} from 'type-fest'; | ||
const settings: Settings = { | ||
textEditor: { | ||
fontSize: 14; | ||
fontColor: '#000000'; | ||
fontWeight: 400; | ||
} | ||
autocomplete: false; | ||
autosave: true; | ||
}; | ||
const applySavedSettings = (savedSettings: PartialDeep<Settings>) => { | ||
return {...settings, ...savedSettings}; | ||
} | ||
settings = applySavedSettings({textEditor: {fontWeight: 500}}); | ||
``` | ||
*/ | ||
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> | ||
: T extends ((...arguments: 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> = { | ||
[PropertyType in keyof ObjectType]: PartialDeep<ObjectType[PropertyType]> | undefined | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {expectType, expectError} from 'tsd'; | ||
import {PartialDeep} from '..'; | ||
|
||
const foo = { | ||
baz: 'fred', | ||
bar: { | ||
function: (_: string): void => {}, | ||
object: {key: 'value'}, | ||
string: 'waldo', | ||
number: 1, | ||
boolean: false, | ||
symbol: Symbol('test'), | ||
null: null, | ||
undefined: undefined, // eslint-disable-line object-shorthand | ||
map: new Map<string, string>(), | ||
set: new Set<string>(), | ||
array: ['foo'], | ||
tuple: ['foo'] as ['foo'], | ||
readonlyMap: new Map<string, string>() as ReadonlyMap<string, string>, | ||
readonlySet: new Set<string>() as ReadonlySet<string>, | ||
readonlyArray: ['foo'] as readonly string[], | ||
readonlyTuple: ['foo'] as const | ||
} | ||
}; | ||
|
||
const partialDeepFoo: PartialDeep<typeof foo> = foo; | ||
|
||
expectError(expectType<Partial<typeof foo>>(partialDeepFoo)); | ||
const partialDeepBar: PartialDeep<typeof foo.bar> = foo.bar; | ||
expectType<typeof partialDeepBar | undefined>(partialDeepFoo.bar); | ||
expectType<((_: string) => void) | undefined>(partialDeepFoo.bar!.function); | ||
expectType<object | undefined>(partialDeepFoo.bar!.object); | ||
expectType<string | undefined>(partialDeepFoo.bar!.string); | ||
expectType<number | undefined>(partialDeepFoo.bar!.number); | ||
expectType<boolean | undefined>(partialDeepFoo.bar!.boolean); | ||
expectType<symbol | undefined>(partialDeepFoo.bar!.symbol); | ||
expectType<null | undefined>(partialDeepFoo.bar!.null); | ||
expectType<undefined>(partialDeepFoo.bar!.undefined); | ||
expectType<Map<string | undefined, string | undefined> | undefined>(partialDeepFoo.bar!.map); | ||
expectType<Set<string | undefined> | undefined>(partialDeepFoo.bar!.set); | ||
expectType<Array<string | undefined> | undefined>(partialDeepFoo.bar!.array); | ||
expectType<['foo' | undefined] | undefined>(partialDeepFoo.bar!.tuple); | ||
expectType<ReadonlyMap<string | undefined, string | undefined> | undefined>(partialDeepFoo.bar!.readonlyMap); | ||
expectType<ReadonlySet<string | undefined> | undefined>(partialDeepFoo.bar!.readonlySet); | ||
expectType<ReadonlyArray<string | undefined> | undefined>(partialDeepFoo.bar!.readonlyArray); | ||
expectType<readonly ['foo' | undefined] | undefined>(partialDeepFoo.bar!.readonlyTuple); |