0
0
Typescriptprogramming~10 mins

Why conditional 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 define a type that returns 'string' if T is a string, otherwise 'number'.

Typescript
type CheckType<T> = T extends [1] ? 'string' : 'number';
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' instead of 'string' in the condition.
2fill in blank
medium

Complete the code to create a type that returns 'true' if T is assignable to number, otherwise 'false'.

Typescript
type IsNumber<T> = T extends [1] ? true : false;
Drag options to blanks, or click blank then click option'
Anumber
Bstring
Cboolean
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' instead of 'number' in the condition.
3fill in blank
hard

Fix the error in the conditional type that should return 'Array' if T is an array, else 'NotArray'.

Typescript
type CheckArray<T> = T extends [1] ? 'Array' : 'NotArray';
Drag options to blanks, or click blank then click option'
AArray
B[]
Cany[]
DArray<any>
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Array' without generic type causes error.
4fill in blank
hard

Fill both blanks to create a conditional type that returns the element type if T is an array, otherwise T itself.

Typescript
type ElementType<T> = T extends [1] ? [2] : T;
Drag options to blanks, or click blank then click option'
Aany[]
BT[number]
CArray<T>
DT[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using Array instead of any[] in condition.
5fill in blank
hard

Fill all three blanks to create a conditional type that returns 'string' if T is string, 'number' if T is number, otherwise 'unknown'.

Typescript
type TypeName<T> = T extends [1] ? 'string' : T extends [2] ? 'number' : [3];
Drag options to blanks, or click blank then click option'
Astring
Bnumber
C'unknown'
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using any instead of 'unknown' in the else case.