0
0
Typescriptprogramming~10 mins

Conditional types for overload replacement 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 Result<T> = T extends number ? [1] : boolean;
Drag options to blanks, or click blank then click option'
Aboolean
Bstring
Cnumber
Dany
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 type that returns Promise<T> if T is not already a Promise.

Typescript
type EnsurePromise<T> = T extends Promise<any> ? T : [1];
Drag options to blanks, or click blank then click option'
Avoid
BT
CPromise<T>
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Returning T directly without wrapping in Promise.
Using 'any' or 'void' incorrectly.
3fill in blank
hard

Fix the error in the conditional type that should extract the return type of a function type F.

Typescript
type ReturnType<F> = F extends (...args: any[]) => [1] ? [1] : never;
Drag options to blanks, or click blank then click option'
Aunknown
Bany
Cvoid
Dinfer R
Attempts:
3 left
💡 Hint
Common Mistakes
Using concrete types like 'any' or 'void' instead of 'infer R'.
Not using 'infer' keyword.
4fill in blank
hard

Fill both blanks to create a type that returns the element type of an array type T, or T itself if not an array.

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

Fill all three blanks to define a type that converts a function type F to a version returning Promise of the original return type.

Typescript
type Asyncify<F> = F extends (...args: infer A) => [1] ? (...args: A) => [2]<[3]> : never;
Drag options to blanks, or click blank then click option'
Ainfer R
BPromise
CR
Dvoid
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'infer R' to extract the return type.
Using 'void' instead of 'Promise'.
Not wrapping the return type in Promise.