Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using T.P instead of T[P]
Forgetting the optional modifier '?'
✗ Incorrect
In a mapped type, [P in keyof T] iterates over keys of T. The syntax T[P] accesses the type of property P in T.
2fill in blank
mediumComplete the code to create a mapped type that makes properties readonly only if they are strings.
Typescript
type ReadonlyStrings<T> = { [P in keyof T]: T[P] extends string ? [1] : T[P] }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'readonly P' instead of 'readonly T[P]'
Forgetting to use 'readonly' keyword
✗ Incorrect
The conditional type checks if T[P] extends string, then makes that property readonly by prefixing with 'readonly'.
3fill in blank
hardFix the error in the mapped type that should exclude properties of type number.
Typescript
type ExcludeNumbers<T> = { [P in keyof T as T[P] extends number ? never : [1]]: T[P] }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'never' instead of 'P' in the else part
Using 'keyof T' which is invalid here
✗ Incorrect
The 'as' clause remaps keys. To keep keys that are not numbers, use 'P' for those keys.
4fill in blank
hardFill both blanks to create a mapped type that makes properties nullable only if they are objects.
Typescript
type NullableObjects<T> = { [P in keyof T]: T[P] extends object ? [1] : [2] }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Making all properties nullable regardless of type
Using 'null' alone instead of 'T[P] | null'
✗ Incorrect
If T[P] is an object, we add '| null' to make it nullable; otherwise, keep the original type.
5fill in blank
hardFill both blanks to create a mapped type that picks only properties whose types extend string or number.
Typescript
type PickStringOrNumber<T> = { [P in keyof T as T[P] extends [1] ? P : T[P] extends [2] ? P : never]: T[P] }; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'boolean' instead of 'number'
Adding extra modifiers to property types
✗ Incorrect
The mapped type uses conditional keys to pick properties of type string or number. The third blank is empty because no modification is needed to the property type.