Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
falsandtru opened this issue Jan 8, 2020 · 8 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@falsandtru
Copy link
Contributor

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:

@falsandtru falsandtru changed the title Can't pick properties using Pick type and a type of keys Pick type can't pick properties using a type of keys Jan 8, 2020
@IllusionMH
Copy link
Contributor

Duplicate of #30825 and may other?

@falsandtru
Copy link
Contributor Author

No. That issue suggested the stricter Omit type but conversely this issue suggests the looser Pick type.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jan 8, 2020
@RyanCavanaugh
Copy link
Member

We can't really change either of them after they've shipped. You're free to use your own definitions if you'd like.

@falsandtru
Copy link
Contributor Author

@RyanCavanaugh However, this change seems to make no breaking changes because this change just looses the constraint, right? I want you to answer this point.

@falsandtru
Copy link
Contributor Author

I can't find the actual problem this improvement makes.

@RyanCavanaugh
Copy link
Member

People complained that the Exclude we added didn't have a similar constraint, so presumably we'd get similar complaints about removing an existing constraint.

@falsandtru
Copy link
Contributor Author

...Do you complain the same thing as that people? Eventually, the current definitions of the Exclude and the Extract have been symmetry. Asymmetric features are a basic anti-pattern. Such features are hard to use. I can't find any relevant reason to hold the Pick type and the Omit type asymmetry.

@falsandtru falsandtru changed the title Pick type can't pick properties using a type of keys Pick type can't use common types such as string, like Omit type Jan 9, 2020
@falsandtru falsandtru changed the title Pick type can't use common types such as string, like Omit type Pick type can't use common key types such as string, like Omit type Jan 9, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants