0
0
Typescriptprogramming~10 mins

Generic array syntax in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic array syntax
Declare generic type T
Create array of type T
Add elements of type T
Access elements as type T
Use array with type safety
This flow shows how to declare a generic array type, add elements, and access them with type safety.
Execution Sample
Typescript
function printArray<T>(arr: T[]): void {
  for (const item of arr) {
    console.log(item);
  }
}

printArray<number>([1, 2, 3]);
This code defines a generic function to print elements of an array of any type, then calls it with a number array.
Execution Table
StepActionEvaluationResult
1Declare generic function printArray<T>T is generic typeFunction ready to accept array of any type
2Call printArray<number> with [1, 2, 3]T is numberArray type is number[]
3Start loop over array elementsitem = 1Print 1
4Next loop iterationitem = 2Print 2
5Next loop iterationitem = 3Print 3
6Loop endsNo more elementsFunction returns void
💡 Loop ends after all elements are printed
Variable Tracker
VariableStartAfter 1After 2After 3Final
arrundefined[1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3]
itemundefined123undefined
Key Moments - 3 Insights
Why do we write <T> after the function name?
The <T> declares a generic type parameter so the function can work with any type, as shown in step 1 of the execution table.
What does T[] mean in the function parameter?
T[] means an array where every element is of type T, ensuring type safety when accessing elements, as seen in steps 3-5.
Why do we specify <number> when calling the function?
Specifying <number> tells TypeScript that T is number here, so the array must contain numbers, matching the argument type in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'item' at step 4?
A1
B2
C3
Dundefined
💡 Hint
Check the 'Evaluation' column at step 4 in the execution table.
At which step does the loop finish iterating over the array?
AStep 3
BStep 5
CStep 6
DStep 2
💡 Hint
Look for the step where the 'Loop ends' action happens in the execution table.
If we change the call to printArray<string>(["a", "b"]), what changes in the execution table?
AThe type T becomes string and array elements are strings
BThe loop will not run
CThe function will print numbers instead
DNo change at all
💡 Hint
Refer to step 2 where T is set to number; changing to string changes T and array type.
Concept Snapshot
Generic array syntax in TypeScript:
- Use <T> to declare a generic type parameter.
- Use T[] to type an array of elements of type T.
- Functions can accept arrays of any type safely.
- Specify type when calling, e.g., printArray<number>([1,2,3]).
- Ensures type safety and flexibility.
Full Transcript
This lesson shows how to use generic array syntax in TypeScript. We declare a generic type parameter T using angle brackets after the function name. Then we use T[] to type an array of elements of that type. The example function printArray<T> takes an array of type T and prints each element. When calling the function, we specify the type, like number, so the array must contain numbers. The execution table traces each step: declaring the function, calling it with a number array, looping over elements, printing each, and ending the loop. The variable tracker shows how the array and loop variable change. Key moments clarify why we use <T>, what T[] means, and why we specify the type when calling. The quiz tests understanding of variable values during loop, loop termination step, and effect of changing the generic type. This concept helps write flexible and type-safe code with arrays in TypeScript.