In TypeScript, you can declare a function using the function
keyword, followed by the function name, a list of parameters in parentheses, and the function body enclosed in curly braces.
function greet(name: string) {
return `Hello, ${name}!`;
}
const message = greet('John');
console.log(message); // Output: Hello, John!
TypeScript allows you to specify the types of function parameters and the return type. This helps in ensuring that the function is used correctly and provides better type checking.
function add(a: number, b: number): number {
return a + b;
}
const result = add(5, 3);
console.log(result); // Output: 8
In the above example, the add
function takes two parameters of type number
and returns a value of type number
.
You can make a function parameter optional by adding a ?
after the parameter name. You can also provide a default value for a parameter using the =
operator.
function greet(name: string, greeting?: string) {
if (greeting) {
return `${greeting}, ${name}!`;
}
return `Hello, ${name}!`;
}
console.log(greet('John')); // Output: Hello, John!
console.log(greet('John', 'Hi')); // Output: Hi, John!
function multiply(a: number, b: number = 1): number {
return a * b;
}
console.log(multiply(5)); // Output: 5
console.log(multiply(5, 3)); // Output: 15
Rest parameters allow you to represent an indefinite number of arguments as an array. You can use the ...
syntax before the parameter name.
function sum(...numbers: number[]): number {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
Function overloading allows you to define multiple function signatures for a single function. This is useful when a function can accept different types or numbers of parameters.
function reverse(input: string): string;
function reverse(input: number[]): number[];
function reverse(input: string | number[]): string | number[] {
if (typeof input === 'string') {
return input.split('').reverse().join('');
} else {
return input.slice().reverse();
}
}
console.log(reverse('hello')); // Output: olleh
console.log(reverse([1, 2, 3])); // Output: [3, 2, 1]
Arrow functions are a concise way to write functions in TypeScript. They have a shorter syntax and do not have their own this
value.
const square = (num: number): number => num * num;
console.log(square(5)); // Output: 25
const numbers = [1, 2, 3];
const squaredNumbers = numbers.map((num) => num * num);
console.log(squaredNumbers); // Output: [1, 4, 9]
TypeScript functions provide a powerful and flexible way to write code with strong typing. By understanding the syntax and typing of functions, you can write more reliable, maintainable, and efficient code. Whether you are using function declarations, arrow functions, or function overloading, TypeScript’s type system helps you catch errors early and provides better developer experience.