Type annotations help you tell the computer what kind of information a function expects. This makes your code safer and easier to understand.
Type annotation on function parameters in Typescript
function functionName(parameterName: type) { // function body }
The colon : after the parameter name is used to add the type.
You can use many types like string, number, boolean, or even custom types.
string called name. It prints a greeting.function greet(name: string) { console.log(`Hello, ${name}!`); }
number called num and returns its square.function square(num: number) { return num * num; }
number and returns a boolean telling if the age is 18 or more.function isAdult(age: number): boolean { return age >= 18; }
This program defines a function that multiplies two numbers. It uses type annotations to make sure both inputs are numbers and the output is also a number. Then it calls the function and prints the result.
function multiply(a: number, b: number): number { return a * b; } const result = multiply(4, 5); console.log(`4 times 5 is ${result}`);
If you pass the wrong type, TypeScript will show an error before running the code.
Type annotations do not change how the program runs; they help you catch mistakes early.
You can add types to return values too, after the parentheses.
Type annotations tell what kind of data a function expects.
They help catch errors and make code easier to read.
Use a colon : after the parameter name to add a type.
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
