Skip to content

Commit 28ad22a

Browse files
author
Vittorio
committed
fix(typescript): improve typings for partials
1 parent 0197d87 commit 28ad22a

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

src/partial/internal.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export type Primitive =
2+
| null
3+
| undefined
4+
| string
5+
| number
6+
| boolean
7+
| symbol
8+
| bigint;
9+
10+
export type BuiltIns = Primitive | Date | RegExp;

src/partial/partial.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
1+
import type { BuiltIns } from './internal';
22

3-
export type PartialDeep<T> = T extends Primitive
4-
? Partial<T>
3+
export type PartialDeep<T> = T extends BuiltIns
4+
? T
55
: T extends Map<infer KeyType, infer ValueType>
66
? PartialMapDeep<KeyType, ValueType>
77
: T extends Set<infer ItemType>
@@ -13,34 +13,29 @@ export type PartialDeep<T> = T extends Primitive
1313
: T extends (...args: any[]) => unknown // eslint-disable-line @typescript-eslint/no-explicit-any
1414
? T | undefined
1515
: T extends object
16-
? PartialObjectDeep<T>
16+
? T extends ReadonlyArray<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
17+
? ItemType[] extends T // Test for arrays (non-tuples) specifically
18+
? readonly ItemType[] extends T // Differentiate readonly and mutable arrays
19+
? ReadonlyArray<PartialDeep<ItemType | undefined>>
20+
: Array<PartialDeep<ItemType | undefined>>
21+
: PartialObjectDeep<T>
22+
: PartialObjectDeep<T>
1723
: unknown;
1824

19-
/**
20-
Same as `PartialDeep`, but accepts only `Map`s and as inputs. Internal helper for `PartialDeep`.
21-
*/
22-
interface PartialMapDeep<KeyType, ValueType>
23-
extends Map<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
25+
type PartialMapDeep<KeyType, ValueType> = {} & Map<
26+
PartialDeep<KeyType>,
27+
PartialDeep<ValueType>
28+
>;
2429

25-
/**
26-
Same as `PartialDeep`, but accepts only `Set`s as inputs. Internal helper for `PartialDeep`.
27-
*/
28-
interface PartialSetDeep<T> extends Set<PartialDeep<T>> {}
30+
type PartialSetDeep<T> = {} & Set<PartialDeep<T>>;
2931

30-
/**
31-
Same as `PartialDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `PartialDeep`.
32-
*/
33-
interface PartialReadonlyMapDeep<KeyType, ValueType>
34-
extends ReadonlyMap<PartialDeep<KeyType>, PartialDeep<ValueType>> {}
32+
type PartialReadonlyMapDeep<KeyType, ValueType> = {} & ReadonlyMap<
33+
PartialDeep<KeyType>,
34+
PartialDeep<ValueType>
35+
>;
3536

36-
/**
37-
Same as `PartialDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `PartialDeep`.
38-
*/
39-
interface PartialReadonlySetDeep<T> extends ReadonlySet<PartialDeep<T>> {}
37+
type PartialReadonlySetDeep<T> = {} & ReadonlySet<PartialDeep<T>>;
4038

41-
/**
42-
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
43-
*/
4439
type PartialObjectDeep<ObjectType extends object> = {
4540
[KeyType in keyof SuppressObjectPrototypeOverrides<ObjectType>]?: PartialDeep<
4641
SuppressObjectPrototypeOverrides<ObjectType>[KeyType]

0 commit comments

Comments
 (0)