0
0
Typescriptprogramming~10 mins

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

Typescript
type CheckType<T> = T extends number ? [1] : boolean;
Drag options to blanks, or click blank then click option'
Astring
Bnumber
Cany
Dboolean
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 code to create a conditional type that returns 'true' if T is assignable to string, otherwise 'false'.

Typescript
type IsString<T> = T extends [1] ? true : false;
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.
Swapping true and false branches.
3fill in blank
hard

Fix the error in the conditional type that should return 'never' if T is 'null', otherwise T.

Typescript
type NonNull<T> = T extends [1] ? never : T;
Drag options to blanks, or click blank then click option'
Avoid
Bundefined
Cnull
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'undefined' instead of 'null' in the condition.
Returning 'never' in the wrong branch.
4fill in blank
hard

Fill both blanks to create a conditional type that returns 'Array<U>' if T is an array of U, otherwise T.

Typescript
type WrapArray<T> = T extends [1] ? Array<[2]> : T;
Drag options to blanks, or click blank then click option'
AArray<infer U>
BU
CT
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'infer' keyword.
Using 'T' instead of 'U' in the second blank.
5fill in blank
hard

Fill the two blanks to create a conditional type that returns the type of the property K in T if it exists, otherwise 'never'.

Typescript
type PropType<T, K extends keyof any> = K extends keyof T ? T[1] : [2];
Drag options to blanks, or click blank then click option'
A[K]
Bnever
C.
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation instead of bracket notation.
Returning 'any' instead of 'never' in the false branch.