Confusing description of Pick mapped type #25976
Labels
Bug
A bug in TypeScript
Domain: lib.d.ts
The issue relates to the different libraries shipped with TypeScript
Good First Issue
Well scoped, documented and has the green light
Help Wanted
You can do this
Milestone
Search Terms:
confusing, pick, description
Problem:
Typescript has so much great functionality, but it gets hard to comprehend them for a not very experienced developer if the descriptions are confusing. Example is the description of Pick mapped property.
Actual behavior:
The description of
type Pick<T, K extends keyof T> = { [P in K]: T[P]; }
says:However, the way I have been assuming the description at first, before I discovered the truth, was:
we need to supply a defined type / interface as a first parameter and another defined type / interface as a second parameter
, i.e.:This does not work and would lead to the error, because the second parameter should contain the keys of ParamOne delineated with
|
, even though the description saysa set of !properties K
, nota set of !keys K
. I understand that the wordproperties
probably refers to T, but hear me out.So, the correct way to get type
{ two: string, three: number }
would be this way:Expected behavior:
Therefore, instead of having:
as a description, would not it be more clearer, especially for the not very experienced developers, if the description would change to:
?
I understand that there is also the definition of the type available for us which looks this way:
But for me as a person who has not been working with typescript for a lot of time, this looked like very complicated and unfamiliar syntax when perceived altogether in a single line, even though some parts, like the one to the right, where the object property is being assigned, were more or less readable.
I hope that future generations of typescript users won't be struggling with mapped types the way I have been and this correction might improve the situation. Thank you!
The text was updated successfully, but these errors were encountered: