0
0
Typescriptprogramming~10 mins

Nested 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 nested conditional type that returns 'string' if T is 'number', otherwise 'boolean'.

Typescript
type NestedType<T> = T extends number ? [1] : boolean;
Drag options to blanks, or click blank then click option'
Anumber
Bany
Cboolean
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' instead of 'string' as the true branch.
Confusing the order of true and false branches.
2fill in blank
medium

Complete the nested conditional type to return 'true' if T is 'string', else check if T is 'boolean' and return 'false', otherwise 'never'.

Typescript
type CheckType<T> = T extends string ? true : T extends boolean ? [1] : never;
Drag options to blanks, or click blank then click option'
Afalse
Btrue
Cnever
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'true' instead of 'false' for the boolean check.
Using 'never' in the wrong branch.
3fill in blank
hard

Fix the error in the nested conditional type to correctly return 'number' if T is 'string', else 'string' if T is 'number', otherwise 'boolean'.

Typescript
type FixType<T> = T extends string ? [1] : T extends number ? string : boolean;
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cany
Dboolean
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'string' instead of 'number' for the first branch.
Mixing up the order of types.
4fill in blank
hard

Fill both blanks to create a nested conditional type that returns 'true' if T is 'string', 'false' if T is 'number', otherwise 'never'.

Typescript
type BooleanCheck<T> = T extends string ? [1] : T extends number ? [2] : never;
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
Cnever
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping true and false values.
Using 'never' in the wrong place.
5fill in blank
hard

Fill all three blanks to define a nested conditional type that returns the uppercase string of T if T is 'string', returns T if T is 'number', otherwise returns 'unknown'.

Typescript
type Transform<T> = T extends string ? [1] : T extends number ? [2] : [3];
Drag options to blanks, or click blank then click option'
AUppercase<T>
BT
Cunknown
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Not using Uppercase for strings.
Returning wrong types for number or default case.