Skip to content

Bug ConditionalPick #1197

@salisbury-espinosa

Description

@salisbury-espinosa

Bug description

import type { ConditionalPick } from 'type-fest';

interface TestInterface {
  a: string;
}

// TS validates that everything is ok - WHY ? 
// ConditionalPick<TestInterface, number> === {} .....

const test123123123: ConditionalPick<TestInterface, number> = {
  sdfsdfsdf: 'asdasd',
};

Activity

som-sm

som-sm commented on Jun 20, 2025

@som-sm
Collaborator

This happens because ConditionalPick currently returns the {} type when no matching key is found.

type TestKeys = ConditionalPick<{a: string}, number>;
//   ^? type TestKeys = {}

In cases like these, the output should probably be never.

self-assigned this
on Jun 20, 2025
benzaria

benzaria commented on Jun 22, 2025

@benzaria
Contributor

@salisbury-espinosa u can use a wrapper at the moment if urgent!

import type {ConditionalKeys, EmptyObject} from 'type-fest'

type ConditionalPick<T, V> = Pick<T, ConditionalKeys<T, V>> extends infer Result
	? Result extends EmptyObject
		? never
		: Result
	: never;

or an IsEqual can do as well:

type ConditionalPick<T, V> = Pick<T, ConditionalKeys<T, V>> extends infer Result
	? IsEqual<{}, Result> extends true
		? never
		: Result
	: never;

Also I think this can settle the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @salisbury-espinosa@som-sm@benzaria

      Issue actions

        Bug ConditionalPick · Issue #1197 · sindresorhus/type-fest