0
0
Typescriptprogramming~10 mins

Parameters type in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parameters type
Function declared with typed parameters
Function called with arguments
TypeScript checks argument types
Function runs
Return value
This flow shows how TypeScript checks the types of function parameters when the function is called, allowing the function to run only if types match.
Execution Sample
Typescript
function greet(name: string) {
  return `Hello, ${name}!`;
}

console.log(greet("Alice"));
Defines a function with a string parameter and calls it with a string argument, printing a greeting.
Execution Table
StepActionParameter 'name' TypeArgument PassedType Check ResultOutput
1Function greet declaredstringN/AN/AN/A
2Function greet calledstring"Alice"MatchN/A
3Function body executesstring"Alice"Match"Hello, Alice!"
4console.log outputsstring"Alice"MatchHello, Alice!
💡 Execution stops after console.log outputs the greeting.
Variable Tracker
VariableStartAfter CallFinal
nameundefined"Alice""Alice"
Key Moments - 2 Insights
Why does TypeScript give an error if I pass a number instead of a string?
Because the function parameter 'name' is typed as string, passing a number fails the type check at step 2 in the execution table.
Can I call the function without any argument?
No, because the parameter 'name' is required and typed as string, skipping it causes a type error before execution.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the type check result when calling greet with "Alice"?
ANo check
BMismatch
CMatch
DError
💡 Hint
See row 2 in the execution_table where argument "Alice" is passed and type check result is shown.
At which step does the function actually produce the output string?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Check the 'Output' column in execution_table row 3 where the function body runs and returns the greeting.
If you pass a number instead of a string, what would happen according to the flow?
ATypeScript shows an error before running
BFunction runs normally
COutput is converted to string automatically
DFunction returns undefined
💡 Hint
Refer to the concept_flow where type mismatch leads to an error before function runs.
Concept Snapshot
Parameters type in TypeScript:
- Define function parameters with types: function f(x: type) {}
- When calling, arguments must match parameter types
- Type mismatch causes compile-time error
- Helps catch bugs early
- Example: function greet(name: string) { ... }
Full Transcript
This visual execution shows how TypeScript checks parameter types when calling a function. The function greet expects a string parameter named 'name'. When called with "Alice", TypeScript confirms the argument matches the string type. The function then runs and returns a greeting string. If a wrong type is passed, TypeScript will show an error before running the function. Variables track the parameter value during execution. This helps beginners understand how parameter types enforce correct usage and prevent errors.