Parameters type helps you tell the computer what kind of information a function expects. This makes your code safer and easier to understand.
0
0
Parameters type in Typescript
Introduction
When you want to make sure a function only gets numbers as input.
When you want to describe that a function needs a string and a number.
When you want to avoid mistakes by checking inputs before running code.
When you want to help others understand what kind of data your function needs.
Syntax
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.
Examples
This function expects a
string called name and prints a greeting.Typescript
function greet(name: string) { console.log(`Hello, ${name}!`); }
This function expects two
number parameters and returns their sum.Typescript
function add(a: number, b: number) { return a + b; }
This function takes a
number and returns a boolean telling if the age is 18 or more.Typescript
function isAdult(age: number): boolean { return age >= 18; }
Sample Program
This program defines a function that multiplies two numbers and prints the result.
Typescript
function multiply(x: number, y: number): number { return x * y; } const result = multiply(4, 5); console.log(`4 times 5 is ${result}`);
OutputSuccess
Important Notes
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.
Summary
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.