What if your functions could warn you about mistakes before you even run your code?
Why Type annotation on function parameters in Typescript? - Purpose & Use Cases
Imagine you are writing a function that adds two numbers, but you forget to check what kind of values are passed in. Sometimes you get strings, sometimes numbers, and sometimes even undefined. This causes your program to behave strangely or crash.
Without clear information about what types your function expects, you spend a lot of time debugging unexpected errors. You might accidentally add a number to a string, or call a function with wrong data. This makes your code fragile and hard to maintain.
Type annotation on function parameters lets you clearly say what kind of data your function expects. This helps catch mistakes early, before running the code. It also makes your code easier to understand and safer to use.
function add(a, b) {
return a + b;
}function add(a: number, b: number): number {
return a + b;
}It enables you to write functions that are reliable and easy to understand, reducing bugs and improving collaboration.
When building a calculator app, type annotations ensure that only numbers are added, preventing errors like adding text or missing inputs.
Type annotations clarify what data a function expects.
They help catch errors before running the program.
This leads to safer and more maintainable code.
Practice
function greet(name: string) tell us?Solution
Step 1: Understand the syntax of type annotation
The colon:afternamemeans we are telling TypeScript what typenameshould be.Step 2: Identify the type specified
The type specified isstring, sonamemust be a string.Final Answer:
The function expectsnameto be a string. -> Option AQuick Check:
Parameter type annotation = string [OK]
- Confusing parameter type with return type
- Ignoring the colon and thinking it's optional
- Assuming no parameters are accepted
age in a TypeScript function?Solution
Step 1: Recall correct syntax for parameter type annotation
TypeScript uses a colon:after the parameter name to specify its type.Step 2: Check each option
function setAge(age: number) {} usesage: number, which is correct. Others use invalid syntax.Final Answer:
function setAge(age: number) {} -> Option BQuick Check:
Parameter type syntax = colon after name [OK]
- Using equals sign instead of colon for type
- Placing type before parameter name
- Adding colon after parentheses instead of parameter
function multiply(x: number, y: number): number {
return x * y;
}
console.log(multiply(3, 4));Solution
Step 1: Understand function parameters and return
The function takes two numbers and returns their product.Step 2: Calculate multiply(3, 4)
3 times 4 equals 12, so the function returns 12.Final Answer:
12 -> Option DQuick Check:
3 * 4 = 12 [OK]
- Confusing string concatenation with multiplication
- Ignoring return type annotation
- Expecting error due to missing type
function greet(name string) {
return `Hello, ${name}`;
}Solution
Step 1: Check parameter syntax
The parametername stringis missing a colon:betweennameandstring.Step 2: Confirm other parts are correct
Backticks and parameter name are valid; return type annotation is optional.Final Answer:
Missing colon between parameter name and type -> Option CQuick Check:
Parameter type needs colon [OK]
- Omitting colon between parameter and type
- Thinking return type is mandatory
- Confusing string quotes with backticks
formatPrice that takes a number price and returns a string with a dollar sign. Which is the correct way to add type annotations to the parameter and return type?Solution
Step 1: Determine parameter type
The parameterpriceshould be a number because it represents a price value.Step 2: Determine return type
The function returns a string with a dollar sign, so return type isstring.Step 3: Check options
function formatPrice(price: number): string { return `$${price}`; } correctly annotatesprice: numberand return type: string. Others have missing or wrong types.Final Answer:
function formatPrice(price: number): string { return `$${price}`; } -> Option AQuick Check:
Parameter number, return string [OK]
- Omitting parameter type
- Wrong return type annotation
- Returning wrong data type
Ready to prove it?
Pass the test and this pattern moves unknown
