TypeScript Function
Tags: typescript, webdev, beginners, tutorial
Published: 2024-05-22 06:27:45

# TYPESCRIPT FUNCTIONS

TypeScript has a specific syntax for typing function parameters and return values.

1. **Return Type**

```ts
function getTime(): number  {
   return new Date().getTime();
};

// with arrow function
const getFullYear = (): number => {
   return new Date().getFullYear();
}

const time: number = getTime();
const year: number = getFullYear();
console.log(time);
console.log(year);
```

2. **Void Return Type**

The type void can be used to indicate a function doesn't return any value.

```ts
function main (): void {
   console.log('Hello world');
}

// arrow functions

const bye = (): void => {
   console.log('Good bye! Crual World!');
}

main();
bye();
```

3. **Parameters**

Function parameters are typed with a similar syntax as variable declarations.
```ts

function multiply (a: number, b: number): number {
   return a * b;
}

// arrow function

const mult = (a: number, b: number): number => a *b;

const product: number = mult(148, 48);
const prod: number = multiply(5, 6);
console.log(product);
console.log(prod);

```

If no parameter type is defined, TypeScript will default to using any, unless additional type information is available as shown in the Default Parameters and Type Alias sections below.

4. **Optional Parameters**

By default TypeScript will assume all parameters are required, but they can be explicitly marked as optional.

```ts

function add (a: number, b: number, c?:number): number {
   return a + b + (c || 0);
}

// Arrow function

const addition = (a: number, b:number, c?:number): number => a + b + (c || 0);

const s: number = add(10, 38);
const sum: number = addition(12, 48, 39);
console.log(s);
console.log(sum);

```

5. **Default Parameters**

For parameters with default values, the default value goes after the type annotation:

```ts
function pow (value: number, exponent: number = 10): number {
   return value ** exponent;
}

// arrow function

const power = (value: number, exponent: number = 2): number => value ** exponent;

const resp: number = power(24, 3);
const result: number = pow(20);
console.log(result);
console.log(resp);
```

TypeScript can also infer the type from the default value.

6. **Named Parameters**

Typing named parameters follows the same pattern as typing normal parameters.

```ts

function divise({ dividend, divisor}: {dividend: number, divisor: number}): number{
   return dividend / divisor;
};

console.log(divise({ dividend: 20, divisor: 4}));

// or we can do this - long way

type ParameterType = {
   dividend: number;
   divisor: number;
};

const diviseN = ({ dividend, divisor }: ParameterType): number => dividend / divisor;

const args: ParameterType = {
   dividend: 204,
   divisor: 24,
};
console.log(diviseN(args));

```

7. **Rest Parameters**

Rest parameters can be typed like normal parameters, but the type must be an array as rest parameters are always arrays.

```ts
function add(a: number, b: number, ...rest: number[]) {
   return a + b + rest.reduce((p:number, c:number) => p + c, 0);
}

// arrow function

const addition = (a:number, b: number, ...rest: number[]): number => {
   return a + b + rest.reduce((ac: number, cv:number) => ac + cv, 0);
}
const arr = [1, 2, 3];
console.log(add(5, 3, ...arr));
console.log(addition(2, 3, ...arr));
```

8. **Type Alias**

Function types can be specified separately from functions with type aliases.

```ts
type Negate = (value: number) => number;

// in this function, the parameter `value` automatically gets assigned the type `number` from the type `Negate`
const negateFunction: Negate = (value) => -Math.abs(value);

console.log(negateFunction(48));
```