Skip to content

Pick type can't use common key types such as string, like Omit type #36074

Closed
@falsandtru

Description

@falsandtru

I'm not sure why the Pick type has the keyof T constraint although the Omit type doesn't have.

TypeScript Version: 3.7.x-dev.20200107

Search Terms:

Code

const sym = Symbol();
class C {
    p = 0;
    [sym]: '';
}
type a = Pick<C, symbol>;
type b = Omit<C, string>;
type Structural<T> = Pick<T, string | number>;

Expected behavior:
pass

type Pick<T, K extends string | number | symbol> = { [P in Extract<keyof T, K>]: T[P]; };
type Omit<T, K extends string | number | symbol> = { [P in Exclude<keyof T, K>]: T[P]; }

Actual behavior:
Type 'symbol' does not satisfy the constraint 'unique symbol | "p"'.(2344)
Type 'string | number' does not satisfy the constraint 'keyof T'.
Type 'string' is not assignable to type 'keyof T'.(2344)

type Pick<T, K extends keyof T> = { [P in K]: T[P]; }
type Omit<T, K extends string | number | symbol> = { [P in Exclude<keyof T, K>]: T[P]; };

Playground Link: http://www.typescriptlang.org/play/index.html?ts=3.8.0-dev.20200107#code/MYewdgzgLgBBCeBbGBeGBlJAjEAbAFAJQDcAUMLgIYQQwDCMA3qTKzAA6owAMZbMAbQSIAugC4YAcklkAvqSjx2AUxiUuABQCWwANYAeOgBo42PAD4yilTCxcA8oi1RDJ6ACctYAOaWFS1XQodwBXYCgQ90pcfQAVc00dA1i3YK9vGAAfGDAQxCxld0sgA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions