0
0
Typescriptprogramming~10 mins

Generic arrow functions in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic arrow functions
Start: Define generic arrow function
Call function with type argument
Function uses generic type
Return value of generic type
Use returned value
This flow shows how a generic arrow function is defined, called with a type, uses that type inside, and returns a value of that type.
Execution Sample
Typescript
const identity = <T>(arg: T): T => arg;
const result = identity<string>("hello");
console.log(result);
Defines a generic arrow function that returns its argument, then calls it with a string and prints the result.
Execution Table
StepActionType Parameter TArgument argReturn ValueOutput
1Define generic arrow function identityT (generic)N/AN/AN/A
2Call identity with T=string and arg="hello"string"hello"Returns "hello"N/A
3Assign return value to resultstring"hello""hello"N/A
4Print resultstring"hello""hello"Prints: hello
💡 Function call completes and output is printed.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
identitygeneric arrow functionunchangedunchangedunchanged
resultundefinedundefined"hello""hello"
Key Moments - 3 Insights
Why do we write <T> before the function parameters?
The <T> declares a generic type parameter so the function can work with any type. See execution_table step 1 where identity is defined with <T>.
How does the function know what type T is when called?
When calling identity<string>("hello"), we specify T as string explicitly. See execution_table step 2 where T is string.
What type does the function return?
It returns the same type T as the argument. In this example, it returns a string. See execution_table step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the type of T when identity is called?
Astring
Bnumber
Cboolean
Dany
💡 Hint
Check execution_table row 2 under 'Type Parameter T'
At which step is the variable 'result' assigned the returned value?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table rows and see when 'result' gets the return value
If we called identity<number>(42) instead, what would change in the execution table?
AReturn value would be a string
BType Parameter T would be number and argument would be 42
COutput would be undefined
DFunction definition would change
💡 Hint
Think about how the generic type and argument relate in execution_table row 2
Concept Snapshot
Generic arrow functions use <T> to accept any type.
Syntax: const func = <T>(arg: T): T => arg;
Call with type: func<string>("text");
Returns the same type as input.
Useful for reusable, type-safe code.
Full Transcript
This example shows a generic arrow function in TypeScript named identity. It uses a generic type parameter T declared before the function parameters. When called, we specify the type T explicitly, for example string. The function takes an argument of type T and returns the same value. The execution table traces defining the function, calling it with a string, assigning the return value to result, and printing it. The variable tracker shows how result changes from undefined to the string "hello". Key moments clarify why <T> is needed, how the type is set on call, and what type is returned. The quiz tests understanding of the generic type usage and variable assignment steps. The snapshot summarizes syntax and behavior for quick reference.