Skip to content

Bug ConditionalPick #1197

Closed
Closed

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

  1. added
    bug
    on Jun 20, 2025
  2. 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.

  3. self-assigned this
    on Jun 20, 2025
  4. 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

bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions