Skip to content

Commit

Permalink
perf(ts): use extends constraints on infer
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Jun 15, 2022
1 parent 20241c5 commit f053e76
Show file tree
Hide file tree
Showing 4 changed files with 439 additions and 25 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@
"dist/node/types/legacy/v4_0.d.ts"
]
},
">=4.1": {
"<4.7": {
"*": [
"dist/node/types/legacy/v4_6.d.ts"
]
},
">=4.7": {
"*": [
"dist/node/types/current/index.d.ts"
]
Expand Down
42 changes: 22 additions & 20 deletions src/types/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,18 @@ type DeepMergeRecordsDefaultHKTInternalPropValueHelper<
K extends PropertyKey,
M,
Acc extends ReadonlyArray<unknown>
> = Ts extends readonly [infer Head, ...infer Rest]
? Head extends Readonly<Record<PropertyKey, unknown>>
? Rest extends readonly [unknown, ...ReadonlyArray<unknown>]
? DeepMergeRecordsDefaultHKTInternalPropValueHelper<
Rest,
K,
M,
[...Acc, ValueOfKey<Head, K>]
>
: [...Acc, ValueOfKey<Head, K>]
: never
> = Ts extends readonly [
infer Head extends Readonly<Record<PropertyKey, unknown>>,
...infer Rest
]
? Rest extends readonly [unknown, ...ReadonlyArray<unknown>]
? DeepMergeRecordsDefaultHKTInternalPropValueHelper<
Rest,
K,
M,
[...Acc, ValueOfKey<Head, K>]
>
: [...Acc, ValueOfKey<Head, K>]
: never;

/**
Expand All @@ -138,15 +139,16 @@ type DeepMergeArraysDefaultHKTHelper<
MF extends DeepMergeMergeFunctionsURIs,
M,
Acc extends ReadonlyArray<unknown>
> = Ts extends readonly [infer Head, ...infer Rest]
? Head extends ReadonlyArray<unknown>
? Rest extends readonly [
ReadonlyArray<unknown>,
...ReadonlyArray<ReadonlyArray<unknown>>
]
? DeepMergeArraysDefaultHKTHelper<Rest, MF, M, [...Acc, ...Head]>
: [...Acc, ...Head]
: never
> = Ts extends readonly [
infer Head extends ReadonlyArray<unknown>,
...infer Rest
]
? Rest extends readonly [
ReadonlyArray<unknown>,
...ReadonlyArray<ReadonlyArray<unknown>>
]
? DeepMergeArraysDefaultHKTHelper<Rest, MF, M, [...Acc, ...Head]>
: [...Acc, ...Head]
: never;

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/deepmerge-custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ declare module "../src/types" {
MF extends DeepMergeMergeFunctionsURIs,
M
> {
readonly CustomArrays2: Ts extends Readonly<readonly [...infer Es]>
? Es extends ReadonlyArray<unknown>
? unknown[]
: never
readonly CustomArrays2: Ts extends Readonly<
readonly [ReadonlyArray<unknown>]
>
? unknown[]
: never;
readonly CustomOthers2: string;
}
Expand Down
Loading

0 comments on commit f053e76

Please sign in to comment.