0
0
Typescriptprogramming~10 mins

Type alias for functions in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Type alias for functions
Define type alias
Use alias to type a function
Call function with correct args
Function runs and returns value
Use returned value
First, we create a type alias for a function signature. Then we use it to type a function. When we call the function, it runs and returns a value typed by the alias.
Execution Sample
Typescript
type Greet = (name: string) => string;

const sayHello: Greet = (name) => {
  return `Hello, ${name}!`;
};

console.log(sayHello("Alice"));
This code defines a type alias for a function that takes a string and returns a string, then uses it to type a function that greets by name.
Execution Table
StepActionEvaluationResult
1Define type alias GreetGreet = (string) => stringType alias created
2Assign sayHello with type GreetsayHello: GreetsayHello typed as function(string) => string
3Call sayHello("Alice")Input: "Alice"Function executes
4Return from sayHelloReturns `Hello, Alice!`Output: "Hello, Alice!"
5console.log outputPrints to consoleHello, Alice!
6EndNo more codeExecution stops
💡 All steps completed, function called and output printed
Variable Tracker
VariableStartAfter 1After 2After 3Final
Greetundefinedtype alias settype alias settype alias settype alias set
sayHelloundefinedundefinedfunction assignedfunction assignedfunction assigned
nameundefinedundefinedundefined"Alice"undefined
return valueundefinedundefinedundefined"Hello, Alice!""Hello, Alice!"
Key Moments - 3 Insights
Why do we use a type alias for a function instead of typing the function directly?
Using a type alias (see step 1 in execution_table) lets us reuse the function signature easily for multiple functions, keeping code clear and consistent.
What happens if the function does not return a string as the alias requires?
TypeScript will show an error at assignment (step 2) because the function's return type must match the alias's return type, ensuring type safety.
Why is the variable 'name' undefined before the function call?
'name' is a parameter local to the function and only gets a value when the function is called (step 3). Before that, it does not exist in the outer scope.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output printed at step 5?
A"Hello, Alice!"
B"Hello, Bob!"
Cundefined
DError
💡 Hint
Check the 'Result' column at step 5 in the execution_table.
At which step does the function parameter 'name' get its value?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Evaluation' column for when 'name' is assigned a value in the execution_table.
If we change the type alias to return number instead of string, what will happen at step 2?
ANo error, function works as before
BTypeScript error because function returns string not number
CFunction returns number automatically
DFunction call fails at runtime
💡 Hint
Refer to key_moments about return type mismatch and step 2 in execution_table.
Concept Snapshot
Type alias for functions:
- Syntax: type Name = (params) => returnType;
- Use alias to type functions for clarity and reuse
- Ensures function matches signature
- Helps catch errors early
- Example: type Greet = (name: string) => string;
Full Transcript
This visual trace shows how to create a type alias for a function in TypeScript. First, we define the alias 'Greet' as a function taking a string and returning a string. Then we assign a function 'sayHello' using this alias. When we call sayHello with 'Alice', the function runs and returns 'Hello, Alice!'. The console.log prints this output. Variables like 'name' only get values during the function call. Using type aliases helps keep code clear and safe by enforcing function signatures.