0
0
Typescriptprogramming~5 mins

Pick type in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the Pick type do in TypeScript?
The Pick type creates a new type by selecting specific properties from an existing type. It helps to focus on only the needed parts of a type.
Click to reveal answer
beginner
How do you use Pick to select properties 'name' and 'age' from a type Person?
You write: <br>type PersonInfo = Pick<Person, 'name' | 'age'>;<br>This creates a new type with only name and age from Person.
Click to reveal answer
intermediate
Why is Pick useful in real projects?
It helps to reuse existing types but only keep the parts you need. This makes code simpler and safer by avoiding extra properties.
Click to reveal answer
intermediate
What happens if you try to Pick a property that does not exist in the original type?
TypeScript will give an error because you can only pick keys that exist in the original type.
Click to reveal answer
advanced
Can Pick be combined with other utility types?
Yes! You can combine Pick with types like Partial or Readonly to create flexible and precise types.
Click to reveal answer
What does Pick<T, K> do in TypeScript?
ACreates a new type with only the properties K from type T
BRemoves properties K from type T
CMakes all properties in T optional
DCombines two types T and K
Which syntax correctly picks properties 'id' and 'title' from type Book?
APick&lt;Book, {id, title}&gt;
BPick&lt;Book, ['id', 'title']&gt;
CPick&lt;Book, 'id' | 'title'&gt;
DPick&lt;Book, id, title&gt;
What error occurs if you pick a property not in the original type?
ARuntime error
BTypeScript error: Type '"wrongKey"' is not assignable to keys of T
CNo error, property is ignored
DWarning but code compiles
Can Pick be used to create a type with all properties optional?
ANo, use <code>Partial</code> for that
BYes, <code>Pick</code> makes properties optional
CYes, but only for string properties
DNo, it removes properties instead
Which utility type can be combined with Pick to make picked properties readonly?
APartial
BExclude
CRecord
DReadonly
Explain how the Pick type works and give an example of its syntax.
Think about choosing only some keys from a bigger object type.
You got /3 concepts.
    Describe a situation where using Pick would make your TypeScript code better.
    Imagine you only need a few details from a big object.
    You got /3 concepts.