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

Created on 22 Feb 2017  路  3Comments  路  Source: microsoft/TypeScript

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.

Declined Suggestion

Most helpful comment

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

All 3 comments

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.

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

dlaberge picture dlaberge  路  3Comments

weswigham picture weswigham  路  3Comments

jbondc picture jbondc  路  3Comments