Skip to content

Commit 8ffc5eb

Browse files
committed
ConditionalPick: Address review feedback
- Use `infer Keys` to avoid evaluating `ConditionalKeys` twice - Use `expectType<never>` instead of `expectNever` to avoid unreachable code warnings
1 parent 3559062 commit 8ffc5eb

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

source/conditional-pick.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ type StringKeysOnly = ConditionalPick<Example, string>;
3939
4040
@category Object
4141
*/
42-
export type ConditionalPick<Base, Condition> = IsNever<ConditionalKeys<Base, Condition>> extends true
43-
? never
44-
: Pick<Base, ConditionalKeys<Base, Condition>>;
42+
export type ConditionalPick<Base, Condition> = ConditionalKeys<Base, Condition> extends infer Keys
43+
? IsNever<Keys> extends true
44+
? never
45+
: Pick<Base, Keys & keyof Base>
46+
: never;
4547

4648
export {};

test-d/conditional-pick.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {expectNever, expectType} from 'tsd';
1+
import {expectType} from 'tsd';
22
import type {ConditionalPick, Primitive} from '../index.d.ts';
33

44
class Awesome {
@@ -29,7 +29,7 @@ expectType<{a: string; c?: string}>(exampleConditionalPickWithUndefined);
2929

3030
// Returns `never` when no keys match the condition
3131
declare const noMatchingKeys: ConditionalPick<Example, number>;
32-
expectNever(noMatchingKeys);
32+
expectType<never>(noMatchingKeys);
3333

3434
declare const noMatchingKeysBoolean: ConditionalPick<{a: string; b: number}, boolean>;
35-
expectNever(noMatchingKeysBoolean);
35+
expectType<never>(noMatchingKeysBoolean);

0 commit comments

Comments
 (0)