0
0
Typescriptprogramming~10 mins

Recursive generic types 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 recursive generic type for a nested array.

Typescript
type NestedArray<T> = T | Array<[1]>;
Drag options to blanks, or click blank then click option'
AT
BArray<T>
CNestedArray<T>
Dany
Attempts:
3 left
💡 Hint
Common Mistakes
Using T instead of the recursive type inside the array.
Using Array which does not allow nested arrays.
2fill in blank
medium

Complete the code to define a recursive generic type for a tree node with children.

Typescript
type TreeNode<T> = { value: T; children?: Array<[1]> };
Drag options to blanks, or click blank then click option'
Aany
BT
CArray<T>
DTreeNode<T>
Attempts:
3 left
💡 Hint
Common Mistakes
Using T instead of TreeNode for children.
Omitting the array wrapper for children.
3fill in blank
hard

Fix the error in the recursive generic type for a linked list node.

Typescript
type LinkedList<T> = { value: T; next: [1] | null };
Drag options to blanks, or click blank then click option'
AT
BLinkedList<T>
CArray<T>
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using T instead of LinkedList for next.
Using Array which is incorrect for linked list.
4fill in blank
hard

Fill both blanks to define a recursive generic type for a JSON value.

Typescript
type JSONValue = string | number | boolean | null | { [key: string]: [1] } | Array<[2]>;
Drag options to blanks, or click blank then click option'
AJSONValue
Bany
Cunknown
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'any' or 'unknown' which loses type safety.
Using different types for object values and array elements.
5fill in blank
hard

Fill all three blanks to define a recursive generic type for a nested dictionary with string keys.

Typescript
type NestedDict<T> = { [key: string]: T | [1]<T> | Array<[2]<T>> }; function processDict<T>(dict: [3]<T>) { /* ... */ }
Drag options to blanks, or click blank then click option'
ANestedDict
BT
DArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using T instead of NestedDict for nested dictionaries.
Using Array instead of NestedDict inside the array.
Mismatching the function parameter type.