Typescript : Type Annotation and Inference.
Tags: typescript, webdev, beginners, programming
Published: 2021-12-14 11:44:11

## What is Typescipt?

By wikipedia definition. 

TypeScript is a programming language developed and maintained by Microsoft. It is a strict syntactical superset of JavaScript and adds optional static typing to the language. TypeScript is designed for the development of large applications and transcompiles to JavaScript. As TypeScript is a superset of JavaScript, existing JavaScript programs are also valid TypeScript programs.

In other words :
Typescript is to make development experince less painful.
Typescript is a typed programming language and it provides early error detection in code.

Type Annotations :

- Code we add to tell Typescript what type of value a variable will refer to.

the code we write right after the variable declaration ':string' and before assignment operator is Type Annotation code.

![type annotation declaration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvej08bhvpgl9nouhfvp.png)
here we are telling typescript that the type of this variable is string and cannot be used with any other type.

![Type annotation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnat70l2sym1anrexwt9.png)

when we hover over the name variable we can see it's type is string.
and if we accidently tries to assign value other than string 
we will get this error

![Annoation error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/313t7k04y0x4cmkdzyey.png)


Type Inference :

- Typescript tries to figureout what type of value a variable refers to.


![Inference declaration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jki0ksocxkx3pi4jlzv1.png)
we can see that we have not written the annotation code after variable declaration, hence type inference is in play and typescript figuring out the type of a variable from the assigned value.

here we can see that on hover over the variable color the type of the color is string which is determined by type inference and also we are getting error while assigning number value to color.


![inference error](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ouixf0sg4tbbzflbqwo.png)


if declaration and initialization are on the same line, Typescript will figure out the type of 'color' for us.

## When to use Type Annotations

1.  **When we declare a variable on one line then initialize it later.**


![Uses-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0q1m59ixfdgc02pbsv55.png)

in the above code what we really need to put inside the 
 variable foundWord is a boolean value true when we find the word inside words array.

![Uses-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwmpk8zvkcwc5k2mijn9.png)

due to the type any, user can accidently assign any value inside variable foundWorld.

Therefore, we need type annotation to make this variable boolean type.


![Uses-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3xlcjlis40hrkn6wn1k.png)

in the above code we can see that now we can see error and user has to put boolean value to work.


![Uses-1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8eokn364ckdp6pt5in50.png)

**2. When we want a variable to have a type that cannot be inferred.**


![Type Annotation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnhayafgauf9tzgw7uow.png)

In the above code, we need to assign false value to variable numberAboveZero if there are no numbers above zero and numbers if there are numbers above zero.

by initialing false we restricted the type of numberAboveZero.


![Type Annotation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4jdyxw1qeikfxevgb0q.png)

In this above code we used a Pipe '|' to seperate two different types which can we assigned in to variable numberAboveZero. therefore we need to use Type Annotation here.

**3. When a function returns the 'any' type and we need to clarify the value.**

- When typescript cannot figureout what return value of a function is, it uses type 'any'.


![Type Annotations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1xbwpmqv76ojxc50fy3.png)

In above code we can see that we are getting any type from JSON.parse() function.


![Type Annotation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2c9h1hp9mxj3jpr9sa1.png)

![Type Annotation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tuaiu1tu99l8sb8tpuya.png)

Here we can accidently assign uncorrect value to the coordinates variable.

to make sure this does not happen we use type annotation on coordinates

![Type Annotation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xr05u85i974w13yhaovg.png)

Now we can see the error while assignment uncorrect type value to variable coordinates.

## **When to use type inference**
Use type inference always except where we need to use Type Annotations.


hit like if you liked reading this article.
and please provide feedback for improvement as this is my first time writing a blog.
Thanks
gauravdwivedi@hotmail.com