0
0
Typescriptprogramming~10 mins

Mapped type with conditional types in Typescript - Interactive Code Practice

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

Complete 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'
A[P]
Attempts:
3 left
💡 Hint
Common Mistakes
Using T.P instead of T[P]
Forgetting the optional modifier '?'
2fill in blank
medium

Complete 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'
AT[P]
Breadonly T[P]
Creadonly P
Dreadonly string
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'readonly P' instead of 'readonly T[P]'
Forgetting to use 'readonly' keyword
3fill in blank
hard

Fix 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'
Astring
Bnever
Ckeyof T
DP
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'never' instead of 'P' in the else part
Using 'keyof T' which is invalid here
4fill in blank
hard

Fill 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'
AT[P] | null
BT[P]
Cnull
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Making all properties nullable regardless of type
Using 'null' alone instead of 'T[P] | null'
5fill in blank
hard

Fill 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'
Astring
Bnumber
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'boolean' instead of 'number'
Adding extra modifiers to property types