Parameters type helps you tell the computer what kind of information a function expects. This makes your code safer and easier to understand.
Parameters type in Typescript
function functionName(parameterName: parameterType) { // function body }
The parameterName is the name you give to the input inside the function.
The parameterType tells what kind of data the function expects, like string, number, or boolean.
string called name and prints a greeting.function greet(name: string) { console.log(`Hello, ${name}!`); }
number parameters and returns their sum.function add(a: number, b: number) { return a + b; }
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 and prints the result.
function multiply(x: number, y: number): number { return x * y; } 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.
You can make parameters optional by adding a question mark, like name?: string.
Using parameter types helps catch mistakes early and makes your code easier to read.
Parameter types tell what kind of data a function expects.
This helps prevent errors and makes code clearer.
Use simple types like string, number, and boolean for parameters.
Practice
function greet(name: string) {}Solution
Step 1: Understand parameter type declaration
In TypeScript, writingname: stringmeans the function expects a string value forname.Step 2: Effect of parameter type
This helps catch errors if a different type is passed and makes the code clearer.Final Answer:
It tells the function what type of value to expect for that parameter. -> Option AQuick Check:
Parameter type = expected value type [OK]
- Thinking parameter type changes return type
- Assuming parameter becomes optional automatically
- Believing parameter type converts data
Solution
Step 1: Check TypeScript parameter syntax
The correct syntax uses a colon between the parameter name and its type:x: number.Step 2: Identify correct option
function add(x: number) {} matches this syntax exactly, others have syntax errors.Final Answer:
function add(x: number) {} -> Option BQuick Check:
Parameter syntax = name: type [OK]
- Omitting colon between name and type
- Placing type before parameter name
- Using equals sign instead of colon
function multiply(a: number, b: number) {
return a * b;
}
console.log(multiply(3, 4));Solution
Step 1: Understand function parameters and operation
The function takes two numbers and returns their product.Step 2: Calculate the output
Callingmultiply(3, 4)returns 3 * 4 = 12.Final Answer:
12 -> Option AQuick Check:
3 * 4 = 12 [OK]
- Concatenating numbers as strings
- Expecting error due to types
- Confusing return value with parameter list
function greet(name string) {
console.log('Hello, ' + name);
}Solution
Step 1: Check parameter syntax
The parameter declarationname stringis missing a colon betweennameandstring.Step 2: Correct syntax
It should bename: stringto be valid TypeScript.Final Answer:
Missing colon between parameter name and type. -> Option DQuick Check:
Parameter syntax requires colon [OK]
- Omitting colon in parameter type
- Thinking parameter type goes after function name
- Assuming function must return value
function setFlag(flag: boolean) {
return flag ? 'ON' : 'OFF';
}Solution
Step 1: Check parameter type expected
The function expects abooleanparameter.Step 2: Analyze each call
Calls withtrue,false, andBoolean(0)are booleans. Passing'true'(a string) causes a type error.Final Answer:
setFlag('true') -> Option CQuick Check:
String passed instead of boolean causes error [OK]
- Passing string instead of boolean
- Confusing Boolean() constructor with string
- Assuming any truthy value is allowed
Ready to prove it?
Pass the test and this pattern moves unknown
