Skip to content

Commit 5eeac02

Browse files
authoredNov 2, 2023
PartialDeep: Ensure it doesn't recurse into prototype properties (#738)
1 parent 9960ba4 commit 5eeac02

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed
 

‎source/partial-deep.d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type {BuiltIns} from './internal';
1+
import type {BuiltIns, UnknownRecord} from './internal';
22

33
/**
44
@see PartialDeep
@@ -69,17 +69,17 @@ export type PartialDeep<T, Options extends PartialDeepOptions = {}> = T extends
6969
? PartialReadonlyMapDeep<KeyType, ValueType, Options>
7070
: T extends ReadonlySet<infer ItemType>
7171
? PartialReadonlySetDeep<ItemType, Options>
72-
: T extends object
73-
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
72+
: T extends UnknownRecord
73+
? PartialObjectDeep<T, Options>
74+
: T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
7475
? Options['recurseIntoArrays'] extends true
7576
? ItemType[] extends T // Test for arrays (non-tuples) specifically
7677
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
7778
? ReadonlyArray<PartialDeep<ItemType | undefined, Options>>
7879
: Array<PartialDeep<ItemType | undefined, Options>>
7980
: PartialObjectDeep<T, Options> // Tuples behave properly
8081
: T // If they don't opt into array testing, just use the original type
81-
: PartialObjectDeep<T, Options>
82-
: unknown;
82+
: T;
8383

8484
/**
8585
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.

‎test-d/partial-deep.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const foo = {
1010
bar: {
1111
function: (_: string): void => undefined,
1212
classConstructor: ClassA,
13+
html: document.createElement('div'),
1314
object: {key: 'value'},
1415
string: 'waldo',
1516
number: 1,
@@ -58,6 +59,8 @@ expectAssignable<ReadonlyMap<string | undefined, string | undefined> | undefined
5859
expectAssignable<ReadonlySet<string | undefined> | undefined>(partialDeepFoo.bar!.readonlySet);
5960
expectType<ReadonlyArray<string | undefined> | undefined>(partialDeepFoo.bar!.readonlyArray);
6061
expectType<readonly ['foo'?] | undefined>(partialDeepFoo.bar!.readonlyTuple);
62+
// Test for https://github.com/sindresorhus/type-fest/issues/651
63+
expectType<HTMLDivElement | undefined>(partialDeepFoo.bar!.html);
6164
// Check for compiling with omitting partial keys
6265
partialDeepFoo = {baz: 'fred'};
6366
partialDeepFoo = {bar: {string: 'waldo'}};

0 commit comments

Comments
 (0)
Please sign in to comment.