Parameter type annotations help you tell the computer what kind of information a function expects. This makes your code clearer and helps catch mistakes early.
Parameter type annotations in Typescript
function functionName(parameterName: type) { // function body }
The colon : after the parameter name is used to add the type.
Common types include string, number, and boolean.
string as the name.function greet(name: string) { console.log(`Hello, ${name}!`); }
number and returns its square.function square(num: number) { return num * num; }
number and returns a boolean.function isAdult(age: number): boolean { return age >= 18; }
This program defines a function that multiplies two numbers. It uses parameter type annotations to ensure both inputs are numbers. 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}`);
Type annotations do not change how the program runs; they help during coding and checking.
If you pass the wrong type, TypeScript will show an error before running the code.
Parameter type annotations tell what kind of data a function expects.
They help catch mistakes and make code easier to understand.
Use a colon : after the parameter name followed by the type.
Practice
What does a parameter type annotation in TypeScript do?
Solution
Step 1: Understand the purpose of parameter type annotations
Parameter type annotations specify the expected data type for each function parameter.Step 2: Identify what this means for the function
This helps the programmer and the TypeScript compiler catch errors if wrong data types are used.Final Answer:
It tells what kind of data a function expects for its parameters. -> Option AQuick Check:
Parameter type annotation = tells expected data type [OK]
- Thinking it changes the function's return type
- Believing it runs the function automatically
- Confusing it with removing parameters
Which of the following is the correct way to add a type annotation for a parameter named age that should be a number?
function checkAge(____) { return age > 18; }Solution
Step 1: Recall TypeScript parameter syntax
In TypeScript, the parameter name is followed by a colon and then the type.Step 2: Match the syntax to the options
Only age: number uses the correct syntax:age: number.Final Answer:
age: number -> Option BQuick Check:
Parameter name : type = correct syntax [OK]
- Putting type before parameter name
- Using equals sign instead of colon
- Using hyphen instead of colon
What will be the output of this TypeScript code?
function greet(name: string) { return `Hello, ${name}!`; }
console.log(greet(42));Solution
Step 1: Check the parameter type annotation
The function expects astringparameter namedname.Step 2: Analyze the argument passed
The argument passed is a number42, which does not match the expectedstringtype.Final Answer:
Type error at compile time -> Option AQuick Check:
Passing wrong type causes compile-time error [OK]
- Thinking it prints 'Hello, 42!' anyway
- Expecting a runtime error instead of compile-time
- Assuming undefined is printed
Identify the error in this TypeScript function and choose the fix:
function multiply(x: number, y) { return x * y; }Solution
Step 1: Check parameter annotations
Parameterxhas a typenumber, butyhas no type annotation.Step 2: Understand TypeScript's requirement
All parameters should have type annotations for clarity and type safety.Final Answer:
Add type annotation toy:y: number-> Option CQuick Check:
All parameters need type annotations [OK]
- Leaving some parameters untyped
- Changing unrelated parameter types
- Thinking return type annotation fixes parameter error
You want to write a function formatPrice that takes a price parameter which can be either a number or a string. How should you annotate the parameter to accept both types?
Solution
Step 1: Understand union types in TypeScript
To accept multiple types, use the union operator|between types.Step 2: Apply union type to parameter
Useprice: number | stringto allow either type.Final Answer:
price: number | string -> Option DQuick Check:
Use|for multiple allowed types [OK]
- Using & instead of | for union types
- Using comma-separated types
- Using any which loses type safety
Ready to prove it?
Pass the test and this pattern moves unknown
