Skip to content

Commit a76b674

Browse files
committed
Fix some type param defaults
1 parent 9037b6b commit a76b674

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

src/index.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,13 @@ export type DeepImmutable<T> = T extends Date
1313
? ReadonlyURL
1414
: T extends URLSearchParams
1515
? 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>
2217
? ImmutableMap<DeepImmutable<K>, DeepImmutable<V>>
2318
: T extends WeakMap<infer K, infer V>
2419
? ReadonlyWeakMap<DeepImmutable<K>, DeepImmutable<V>>
2520
: T extends ReadonlyWeakMap<infer K, infer V>
2621
? 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>
3323
? ImmutableSet<DeepImmutable<U>>
3424
: T extends WeakSet<infer U>
3525
? ReadonlyWeakSet<DeepImmutable<U>>
@@ -65,7 +55,7 @@ export type ImmutableShallow<T extends {}> = {
6555
// https://github.com/agiledigital/readonly-types/issues/518
6656
export type ImmutableArray<T> = ImmutableShallow<ReadonlyArray<T>>;
6757

68-
export type NonEmptyImmutableArray<T> = ImmutableShallow<
58+
export type ImmutableNonEmptyArray<T> = ImmutableShallow<
6959
readonly [T, ...(readonly T[])]
7060
>;
7161

@@ -81,6 +71,12 @@ export type AnyArray<T> =
8171
// eslint-disable-next-line functional/prefer-readonly-type
8272
| Array<T>;
8373

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+
8480
/**
8581
* Recursive machinery to implement PrincipledArray's flat method. Copied from TypeScript standard lib
8682
* with necessary changes to accommodate our array types.
@@ -210,7 +206,7 @@ export type PrincipledArray<T> = ImmutableShallow<
210206
thisArg?: This
211207
) => boolean;
212208

213-
readonly reduce: <U>(
209+
readonly reduce: <U = T>(
214210
callback: (
215211
previousValue: U,
216212
currentValue: T,
@@ -220,7 +216,7 @@ export type PrincipledArray<T> = ImmutableShallow<
220216
initialValue: U
221217
) => U;
222218

223-
readonly reduceRight: <U>(
219+
readonly reduceRight: <U = T>(
224220
callback: (
225221
previousValue: U,
226222
currentValue: T,
@@ -238,26 +234,26 @@ export type PrincipledArray<T> = ImmutableShallow<
238234
*/
239235
export type PrincipledNonEmptyArray<T> = ImmutableShallow<
240236
OmitStrict<
241-
NonEmptyImmutableArray<T> & PrincipledArray<T>,
237+
ImmutableNonEmptyArray<T> & PrincipledArray<T>,
242238
"reduce" | "reduceRight"
243239
>
244240
> & {
245-
readonly reduce: <U>(
241+
readonly reduce: <U = T>(
246242
callback: (
247243
previousValue: U,
248244
currentValue: T,
249245
currentIndex: number,
250-
array: PrincipledArray<T>
246+
array: PrincipledNonEmptyArray<T>
251247
) => U,
252248
initialValue?: U | undefined
253249
) => U;
254250

255-
readonly reduceRight: <U>(
251+
readonly reduceRight: <U = T>(
256252
callback: (
257253
previousValue: U,
258254
currentValue: T,
259255
currentIndex: number,
260-
array: PrincipledArray<T>
256+
array: PrincipledNonEmptyArray<T>
261257
) => U,
262258
initialValue?: U | undefined
263259
) => U;
@@ -274,7 +270,7 @@ export const principledArray = <T>(
274270
};
275271

276272
export const principledNonEmptyArray = <T>(
277-
immutableArray: NonEmptyImmutableArray<T>
273+
immutableArray: ImmutableNonEmptyArray<T>
278274
): PrincipledNonEmptyArray<T> => {
279275
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
280276
return [...immutableArray] as unknown as PrincipledNonEmptyArray<T>;

0 commit comments

Comments
 (0)