0
0
Typescriptprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Generic function syntax
Define generic function with <T>
Call function with specific type
Function uses type T inside
Return value of type T
End
This flow shows how a generic function is defined with a type placeholder T, then called with a specific type, uses that type inside, and returns a value of that type.
Execution Sample
Typescript
function identity<T>(arg: T): T {
  return arg;
}

const output = identity<string>("hello");
console.log(output);
This code defines a generic function identity that returns the argument it receives, then calls it with a string and prints the result.
Execution Table
StepActionType TArgument (arg)Return ValueOutput
1Define function identity with generic type TT (generic)N/AN/AN/A
2Call identity with T = string and arg = "hello"string"hello"N/AN/A
3Inside function, return argstring"hello""hello"N/A
4Assign return value to outputstringN/A"hello"N/A
5Print outputstringN/AN/A"hello"
💡 Function completes after returning the argument; output is printed.
Variable Tracker
VariableStartAfter CallAfter ReturnFinal
argundefined"hello""hello"undefined (out of scope)
outputundefinedundefined"hello""hello"
Key Moments - 3 Insights
Why do we write <T> after the function name?
The <T> declares a placeholder type that the function can use inside. It means the function works with any type, not just one fixed type. See execution_table step 1.
How does the function know what type T is?
When we call the function, we specify the type, like identity<string>. This tells the function what T means for this call. See execution_table step 2.
What type does the function return?
It returns the same type T that was passed in, so if T is string, it returns a string. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'arg' at step 3?
Aundefined
B"hello"
CT (generic)
Dnull
💡 Hint
Check the 'Argument (arg)' column at step 3 in the execution_table.
At which step is the output variable assigned a value?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column for when output gets a value in the execution_table.
If we call identity<number>(5) instead, what would be the type of output?
Aany
Bstring
Cnumber
Dboolean
💡 Hint
Refer to the explanation in key_moments about how T is set at call time.
Concept Snapshot
Generic function syntax:
function funcName<T>(arg: T): T {
  return arg;
}
- <T> declares a type placeholder.
- Caller specifies actual type.
- Function uses T inside and returns T.
- Enables reusable, type-safe functions.
Full Transcript
This visual execution shows how a generic function in TypeScript works. First, the function identity is defined with a generic type T. Then, it is called with T set to string and argument "hello". Inside the function, the argument is returned as the same type T. The returned value is assigned to output and printed. Variables arg and output change as the function runs. Key points include that <T> declares a placeholder type, the caller sets the actual type, and the function returns the same type. The quizzes test understanding of variable values at steps and how types are assigned.