0
0
Typescriptprogramming~10 mins

Generic interface for collections in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic interface for collections
Define generic interface<T>
Create collection implementing interface
Use collection with specific type
Access elements with type safety
Perform operations (add, get, size)
End
This flow shows how a generic interface is defined, implemented by a collection, and then used with specific types to ensure type safety.
Execution Sample
Typescript
interface Collection<T> {
  add(item: T): void;
  get(index: number): T | undefined;
  size(): number;
}

class MyCollection<T> implements Collection<T> {
  private items: T[] = [];
  add(item: T): void { this.items.push(item); }
  get(index: number): T | undefined { return this.items[index]; }
  size(): number { return this.items.length; }
}

const numbers = new MyCollection<number>();
numbers.add(10);
numbers.add(20);
console.log(numbers.get(1));
Defines a generic collection interface and a class implementing it, then uses it with numbers and prints the second element.
Execution Table
StepActionVariable/MethodInputOutput/State
1Create instancenumbersMyCollection<number>Empty array []
2Call addnumbers.add10items = [10]
3Call addnumbers.add20items = [10, 20]
4Call getnumbers.get1Returns 20
5Print outputconsole.log20Output: 20
💡 All steps executed; program ends after printing 20.
Variable Tracker
VariableStartAfter 1After 2Final
numbers.items[][10][10, 20][10, 20]
Key Moments - 3 Insights
Why do we use <T> in the interface and class?
The <T> is a placeholder for any type. It lets us write one interface and class that work with any type, like numbers or strings, ensuring type safety as shown in the execution_table steps 1 and 2.
What happens if we try to add a string to numbers collection?
TypeScript will give an error because numbers collection expects only numbers. This is because of the generic type <number> used when creating the instance, as seen in step 1.
Why does get return T | undefined?
Because if the index is out of range, there is no item to return, so it returns undefined. This prevents runtime errors and is shown in the method signature in the code sample.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of numbers.items after step 3?
A[]
B[10, 20]
C[10]
D[20]
💡 Hint
Check the 'Output/State' column in row 3 of the execution_table.
At which step does the program output the value 20?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look for the 'Print output' action in the execution_table.
If we change MyCollection<number> to MyCollection<string>, what type of values can we add?
AOnly strings
BOnly numbers
CAny type
DOnly booleans
💡 Hint
Refer to the generic type parameter used in the variable_tracker and code sample.
Concept Snapshot
Generic interface syntax:
interface Collection<T> {
  add(item: T): void;
  get(index: number): T | undefined;
  size(): number;
}

Use <T> to make interfaces reusable for any type.
Implement with class<T> and specify type when creating instance.
Ensures type safety and flexibility.
Full Transcript
This example shows how to define a generic interface Collection with a type parameter T. The interface has methods to add items, get items by index, and get the size. A class MyCollection implements this interface using an internal array. We create an instance numbers of type MyCollection<number> and add two numbers: 10 and 20. Then we get the item at index 1, which is 20, and print it. The execution table traces each step, showing how the internal array changes and when output occurs. Key points include understanding the generic type parameter <T> which allows the interface and class to work with any type, and how TypeScript enforces type safety by preventing adding wrong types. The visual quiz tests understanding of the state after adding items, when output happens, and how changing the generic type affects allowed values.