@@ -13,23 +13,13 @@ export type DeepImmutable<T> = T extends Date
13
13
? ReadonlyURL
14
14
: T extends URLSearchParams
15
15
? ReadonlyURLSearchParams
16
- : // eslint-disable-next-line functional/prefer-readonly-type
17
- T extends Map < infer K , infer V >
18
- ? ImmutableMap < DeepImmutable < K > , DeepImmutable < V > >
19
- : T extends ReadonlyMap < infer K , infer V >
20
- ? ImmutableMap < DeepImmutable < K > , DeepImmutable < V > >
21
- : T extends ImmutableMap < infer K , infer V >
16
+ : T extends AnyMap < infer K , infer V >
22
17
? ImmutableMap < DeepImmutable < K > , DeepImmutable < V > >
23
18
: T extends WeakMap < infer K , infer V >
24
19
? ReadonlyWeakMap < DeepImmutable < K > , DeepImmutable < V > >
25
20
: T extends ReadonlyWeakMap < infer K , infer V >
26
21
? ReadonlyWeakMap < DeepImmutable < K > , DeepImmutable < V > >
27
- : // eslint-disable-next-line functional/prefer-readonly-type
28
- T extends Set < infer U >
29
- ? ImmutableSet < DeepImmutable < U > >
30
- : T extends ReadonlySet < infer U >
31
- ? ImmutableSet < DeepImmutable < U > >
32
- : T extends ImmutableSet < infer U >
22
+ : T extends AnySet < infer U >
33
23
? ImmutableSet < DeepImmutable < U > >
34
24
: T extends WeakSet < infer U >
35
25
? ReadonlyWeakSet < DeepImmutable < U > >
@@ -65,7 +55,7 @@ export type ImmutableShallow<T extends {}> = {
65
55
// https://github.com/agiledigital/readonly-types/issues/518
66
56
export type ImmutableArray < T > = ImmutableShallow < ReadonlyArray < T > > ;
67
57
68
- export type NonEmptyImmutableArray < T > = ImmutableShallow <
58
+ export type ImmutableNonEmptyArray < T > = ImmutableShallow <
69
59
readonly [ T , ...( readonly T [ ] ) ]
70
60
> ;
71
61
@@ -81,6 +71,12 @@ export type AnyArray<T> =
81
71
// eslint-disable-next-line functional/prefer-readonly-type
82
72
| Array < T > ;
83
73
74
+ // eslint-disable-next-line functional/prefer-readonly-type
75
+ export type AnyMap < K , V > = Map < K , V > | ReadonlyMap < K , V > | ImmutableMap < K , V > ;
76
+
77
+ // eslint-disable-next-line functional/prefer-readonly-type, functional/type-declaration-immutability
78
+ export type AnySet < T > = Set < T > | ReadonlySet < T > | ImmutableSet < T > ;
79
+
84
80
/**
85
81
* Recursive machinery to implement PrincipledArray's flat method. Copied from TypeScript standard lib
86
82
* with necessary changes to accommodate our array types.
@@ -210,7 +206,7 @@ export type PrincipledArray<T> = ImmutableShallow<
210
206
thisArg ?: This
211
207
) => boolean ;
212
208
213
- readonly reduce : < U > (
209
+ readonly reduce : < U = T > (
214
210
callback : (
215
211
previousValue : U ,
216
212
currentValue : T ,
@@ -220,7 +216,7 @@ export type PrincipledArray<T> = ImmutableShallow<
220
216
initialValue : U
221
217
) => U ;
222
218
223
- readonly reduceRight : < U > (
219
+ readonly reduceRight : < U = T > (
224
220
callback : (
225
221
previousValue : U ,
226
222
currentValue : T ,
@@ -238,26 +234,26 @@ export type PrincipledArray<T> = ImmutableShallow<
238
234
*/
239
235
export type PrincipledNonEmptyArray < T > = ImmutableShallow <
240
236
OmitStrict <
241
- NonEmptyImmutableArray < T > & PrincipledArray < T > ,
237
+ ImmutableNonEmptyArray < T > & PrincipledArray < T > ,
242
238
"reduce" | "reduceRight"
243
239
>
244
240
> & {
245
- readonly reduce : < U > (
241
+ readonly reduce : < U = T > (
246
242
callback : (
247
243
previousValue : U ,
248
244
currentValue : T ,
249
245
currentIndex : number ,
250
- array : PrincipledArray < T >
246
+ array : PrincipledNonEmptyArray < T >
251
247
) => U ,
252
248
initialValue ?: U | undefined
253
249
) => U ;
254
250
255
- readonly reduceRight : < U > (
251
+ readonly reduceRight : < U = T > (
256
252
callback : (
257
253
previousValue : U ,
258
254
currentValue : T ,
259
255
currentIndex : number ,
260
- array : PrincipledArray < T >
256
+ array : PrincipledNonEmptyArray < T >
261
257
) => U ,
262
258
initialValue ?: U | undefined
263
259
) => U ;
@@ -274,7 +270,7 @@ export const principledArray = <T>(
274
270
} ;
275
271
276
272
export const principledNonEmptyArray = < T > (
277
- immutableArray : NonEmptyImmutableArray < T >
273
+ immutableArray : ImmutableNonEmptyArray < T >
278
274
) : PrincipledNonEmptyArray < T > => {
279
275
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
280
276
return [ ...immutableArray ] as unknown as PrincipledNonEmptyArray < T > ;
0 commit comments