Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to infer the type of the first element in a tuple.
Typescript
type First<T extends any[]> = T extends [[1], ...any[]] ? [1] : never;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a concrete type instead of
infer.Not using the spread operator
...any[] to match the rest of the tuple.✗ Incorrect
The infer keyword is used to declare a type variable U that captures the first element's type in the tuple T.
2fill in blank
mediumComplete the code to infer the return type of a function type.
Typescript
type ReturnType<T> = T extends (...args: any[]) => [1] ? [1] : never;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed type like
void instead of infer.Not matching the function signature correctly.
✗ Incorrect
The infer R captures the return type of the function T.
3fill in blank
hardFix the error in the code to infer the element type of an array type.
Typescript
type ElementType<T> = T extends Array<[1]> ? [1] : never;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a plain type variable without
infer.Using
any which loses type information.✗ Incorrect
infer E is needed to capture the element type inside the array type T.
4fill in blank
hardFill both blanks to infer the argument types of a function type as a tuple.
Typescript
type Args<T> = T extends (...args: [1]) => any ? [2] : never;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed type like
any[] instead of infer.Using different type variables in the two blanks.
✗ Incorrect
infer A captures the tuple of argument types from the function type T.
5fill in blank
hardFill all three blanks to infer the resolved type from a Promise type.
Typescript
type UnwrapPromise<T> = T extends Promise<[1]> ? [1] : T extends Promise<[2]> ? [3] : T;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different type variables for the two Promise checks.
Not using
infer to capture the inner type.✗ Incorrect
The first infer U captures the type inside the Promise. Then U is used to return the resolved type.