Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions source/conditional-pick-deep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Assert the condition according to the {@link ConditionalPickDeepOptions.conditio
*/
type AssertCondition<Type, Condition, Options extends ConditionalPickDeepOptions> = Options['condition'] extends 'equality'
? IsEqual<Type, Condition>
: Type extends Condition
: [Type] extends [Condition]
? true
: false;

Expand Down Expand Up @@ -114,7 +114,7 @@ type _ConditionalPickDeep<
> = ConditionalSimplifyDeep<ConditionalExcept<{
[Key in keyof Type]: AssertCondition<Type[Key], Condition, Options> extends true
? Type[Key]
: IsPlainObject<Type[Key]> extends true
: IsPlainObject<NonNullable<Type[Key]>> extends true
? _ConditionalPickDeep<Type[Key], Condition, Options>
: typeof conditionalPickDeepSymbol;
}, (typeof conditionalPickDeepSymbol | undefined) | EmptyObject>, never, UnknownRecord>;
Expand Down
36 changes: 35 additions & 1 deletion test-d/conditional-pick-deep.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import type {ConditionalPickDeep} from '../index.d.ts';
import type {ConditionalPickDeep, Paths} from '../index.d.ts';

declare class ClassA {
public a: string;
Expand All @@ -10,6 +10,11 @@ interface InterfaceA {
a: number;
}

enum TestEnum {
TEST1 = 'TEST1',
TEST2 = 'TEST2',
}

type Example = {
optional?: boolean;
optionalWithUndefined?: boolean | undefined;
Expand Down Expand Up @@ -42,6 +47,15 @@ type Example = {
};
};

type EnumExample = {
enum: TestEnum;
string: string | null;
};

type UnionExample = {
a: string | null;
};

declare const stringPick: ConditionalPickDeep<Example, string>;
expectType<{
literal: 'foo';
Expand Down Expand Up @@ -158,3 +172,23 @@ expectType<{set: Set<string>; never: never}>(setPick);

declare const interfaceTest: ConditionalPickDeep<Example, InterfaceA>;
expectType<{interface: InterfaceA; never: never}>(interfaceTest);

declare const enumPick: ConditionalPickDeep<EnumExample, TestEnum>;
expectType<{enum: TestEnum}>(enumPick);

expectType<'enum'>({} as Paths<ConditionalPickDeep<EnumExample, TestEnum>>);

declare const unionPick:
ConditionalPickDeep<UnionExample, TestEnum>;
expectType<never>(unionPick);

type NestedEnumExample = {
obj: {
value: TestEnum;
other: string | null;
};
};

declare const nestedPick:
ConditionalPickDeep<NestedEnumExample, TestEnum>;
expectType<{obj: {value: TestEnum}}>(nestedPick);
Loading