Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'number' instead of 'string' as the true branch.
Confusing the order of true and false branches.
✗ Incorrect
The conditional type checks if T extends number; if yes, it returns string, else boolean.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning T directly without wrapping in Promise.
Using 'any' or 'void' incorrectly.
✗ Incorrect
If T is already a Promise, return T; otherwise wrap T in Promise.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using concrete types like 'any' or 'void' instead of 'infer R'.
Not using 'infer' keyword.
✗ Incorrect
Using
infer R allows extracting the return type from the function type.4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'T[]' instead of 'Array' in the extends clause.
Not using 'infer' to extract the element type.
✗ Incorrect
The conditional checks if T is an array type and extracts its element type using
infer U.5fill in blank
hardFill 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'
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.
✗ Incorrect
The type extracts the return type R, then returns a function with the same arguments but returning Promise.