# TypeScript Unlocked: Functions

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">This article is a part of <a target="_blank" rel="noopener noreferrer nofollow" href="https://blog.troncodes.com/series/typescript-unlocked" style="pointer-events: none">TypeScript Unlocked</a> Series on my blog. Every article in the series is short, crisp and filled with examples and code snippets. If you want to learn TypeScript from scratch for absolutely FREE!!! check it out.</div>
</div>

## Function Anatomy for typescript

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712588597396/2d806c34-717b-4bec-be49-843e88c69ea0.png align="center")

Looking at the above image you can see how we do type checking when we write a function. If you don’t specify any type for the function parameter or function return type it will default to type of any which is not recommended when using type script.

## Understating how types work in functions

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712588744985/46b99fcc-0018-498d-abc1-a75e892fd788.png align="center")

If you have understood how the type checking is done for a variable understanding this will be very simple. Lets see how the types work in function.

## Parameter type

when you specify the parameter type typescript will warn you if you try to perform operation on that particular parameter which is outside the scope of its type. for example if you have specified the type as **number** and you are trying to apply **string** methods on the parameter you’ll get a red line.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712588886077/7ebc6c03-3dc8-42bc-ada7-463b468e56ed.png align="center")

## Return type

when return type of a function is specified, you can’t return a value from the function which is other than the specified type. for example you cant return a **string** if you have specified the return type as a **number**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712588946200/76606426-44d9-4134-94dc-5f138418e37a.png align="center")

## Arrow Function Anatomy for TypeScript

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712589036511/63aff673-45d3-4a61-8d7f-3fafe6a76b96.png align="center")

Every functionality remains the same expect the minor syntax changes

## Using types in map or any call back function

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712589100518/e5b1b527-09d3-4fd2-8edc-964d8d557508.png align="center")

it’s the same syntax we use for arrow function with only difference that the function is being placed inside another function.

actually it’s not required to specify the type when we are using the map function because **TypeScript** is smart enough to know the type value we’ll be having for the parameter by reading the **array** on which we are using the map.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712589198631/596bc124-bae0-4246-95b2-a1a6c62a1e7c.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1712589220346/1f2b8ad9-40e5-41b3-b3f6-6cf5c33c1e29.png align="left")

if you don’t specify the type and hover over the variable you can see that it already knows what type is it (I'm using VS code).

## Never Type

We use **never** when we want to specify that the function should not return anything not even void. This is helpful when we want to call the function which terminates our program or just raises and error.

```typescript
function rainsError(errMsg: string): never{
    throw new Error(errMsg);
}
```

if you reached till here and you liked what you read don't forget to share it with other people. if you have any feedback or suggestions feel free to use the comment section. Hope you tag along till the end of this series and strengthen your coding armor by adding **TypeScript** to it. Happy coding!
