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

Should 'Partial<T>' and Readonly<T> should have their type parameters constrained to 'object'? #14249

Closed
DanielRosenwasser opened this issue Feb 22, 2017 · 3 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 22, 2017

Given #14224, I think that Partial should be defined as follows:

type Partial<T extends object> = {
    [K in keyof T]?: T[K]
}

Readonly could be defined similarly.

I don't necessarily know if Pick should be defined that way. Part of me thinks that this Pick has a different intent.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 22, 2017

mapped type in general handle primitives differently. but i agree that these types should not be used on a primitive.

the problem now is in any generic function every-time you write Partial<T> you have to constrain T extends object.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Feb 22, 2017
@Knagis
Copy link
Contributor

Knagis commented Feb 24, 2017

I have recently used Partial<> like this:

type Foo<T> = {
  [K in keyof T]: Partial<T[K]>
}

Wouldn't the constraint prohibit this? Of course, I could create my own Partial<> definition that skips the constraint.

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed In Discussion Not yet reached consensus labels Mar 7, 2017
@RyanCavanaugh
Copy link
Member

Too annoying to force authors to constrain their own type parameters to object if we did this

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants