0
0
Typescriptprogramming~10 mins

Inferring types with infer keyword 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 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'
Ainfer R
Binfer T
Cinfer U
Dinfer V
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.
2fill in blank
medium

Complete 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'
Ainfer R
Bvoid
Cany
Dunknown
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed type like void instead of infer.
Not matching the function signature correctly.
3fill in blank
hard

Fix 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'
AE
Binfer E
Cinfer T
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using a plain type variable without infer.
Using any which loses type information.
4fill in blank
hard

Fill 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'
Ainfer A
Bany[]
Cinfer R
Dunknown[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed type like any[] instead of infer.
Using different type variables in the two blanks.
5fill in blank
hard

Fill 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'
Ainfer U
Bany
CU
Dunknown
Attempts:
3 left
💡 Hint
Common Mistakes
Using different type variables for the two Promise checks.
Not using infer to capture the inner type.