0
0
Typescriptprogramming~10 mins

Why mapped types are needed in Typescript - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a mapped type that makes all properties optional.

Typescript
type PartialType<T> = { [P in keyof T]?: T[1] };
Drag options to blanks, or click blank then click option'
A[P]
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use keyof to get keys.
Using incorrect syntax for mapped types.
2fill in blank
medium

Complete the code to create a mapped type that makes all properties readonly.

Typescript
type ReadonlyType<T> = { readonly [P in keyof T][1] T[P] };
Drag options to blanks, or click blank then click option'
A=
B:
C=>
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using equals = instead of colon :.
Using arrow => which is for functions.
3fill in blank
hard

Fix the error in the mapped type that tries to change all properties to string.

Typescript
type Stringify<T> = { [P in keyof T]: [1] };
Drag options to blanks, or click blank then click option'
Anumber
BT[P]
Cany
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using T[P] which keeps original types.
Using unrelated types like number.
4fill in blank
hard

Fill both blanks to create a mapped type that makes all properties nullable.

Typescript
type Nullable<T> = { [P in [1]]: T[P][2] null };
Drag options to blanks, or click blank then click option'
Akeyof T
B| null
CT
D& null
Attempts:
3 left
💡 Hint
Common Mistakes
Using T instead of keyof T for keys.
Using & null which is intersection, not union.
5fill in blank
hard

Fill all three blanks to create a mapped type that picks only string properties from a type.

Typescript
type PickStrings<T> = { [[1] in keyof T as T[[2]] extends [3] ? [1] : never]: T[[1]] };
Drag options to blanks, or click blank then click option'
AP
Cstring
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for keys in the two places.
Using number instead of string in the condition.