Skip to content

Commit 6af847a

Browse files
authored
ConditionalPickDeep: Fix returning {} instead of never when no keys match (#1360)
1 parent 24be93d commit 6af847a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

source/conditional-pick-deep.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type BooleanPick = ConditionalPickDeep<Example, boolean | undefined>;
7474
//=> {c: {e: {g?: boolean}; j: boolean}}
7575
7676
type NumberPick = ConditionalPickDeep<Example, number>;
77-
//=> {}
77+
//=> never
7878
7979
type StringOrBooleanPick = ConditionalPickDeep<Example, string | boolean>;
8080
//=> {
@@ -99,11 +99,13 @@ export type ConditionalPickDeep<
9999
Type,
100100
Condition,
101101
Options extends ConditionalPickDeepOptions = {},
102-
> = _ConditionalPickDeep<
102+
> = _NeverIfEmpty<_ConditionalPickDeep<
103103
Type,
104104
Condition,
105105
ApplyDefaultOptions<ConditionalPickDeepOptions, DefaultConditionalPickDeepOptions, Options>
106-
>;
106+
>>;
107+
108+
type _NeverIfEmpty<Type> = Type extends EmptyObject ? never : Type;
107109

108110
type _ConditionalPickDeep<
109111
Type,

test-d/conditional-pick-deep.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ declare const emptyPick: ConditionalPickDeep<Example, 'abcdefg'>;
102102
expectType<{never: never}>(emptyPick);
103103

104104
declare const emptyEqualityPick: ConditionalPickDeep<Example, 'abcdefg', {condition: 'equality'}>;
105-
expectType<{}>(emptyEqualityPick);
105+
expectType<never>(emptyEqualityPick);
106+
107+
// Returns `never` when no keys match the condition
108+
declare const noMatchingKeys: ConditionalPickDeep<{a: string; b: number}, boolean>;
109+
expectType<never>(noMatchingKeys);
110+
111+
// Union with no common properties
112+
declare const unionNoCommon: ConditionalPickDeep<{a: string} | {b: string}, string>;
113+
expectType<{a: string} | {b: string}>(unionNoCommon);
106114

107115
declare const stringOrBooleanPick: ConditionalPickDeep<Example, string | boolean>;
108116
expectType<{

0 commit comments

Comments
 (0)