Introduction To Type Inference In Typescript And Its Best Practices
loader image
Type Inference in TypeScript and Its Best Practices
Web Development

Type Inference in TypeScript and Its Best Practices

Typescript is one of the rapidly growing languages, it is a superset of Javascript which gives us the power of strict typing for Javascript. It helps a developer to easily identify issues that can otherwise only be found in certain critical scenarios.

Typescript is very powerful and flexible when you compare it with other strictly typed languages. Type Inference is one of the features that gives great power and flexibility to typescript. 

How types are associated with a variable 

In TypeScript, you can set the type of a variable in two different types. They are, 

1. Explicit 

This is when you define a type of a variable explicitly while creating the variable. 

2. Implicit

This is when you don’t mention a type explicitly and typescript tries to infer the type from the value you assigned to the variable.

What is Type Inference?

When there is no explicit type that is annotated to a variable typescript, the compiler tries to infer its type from the value or from the usage of the variable. This is known as type inference. Type inference takes place when initializing variables, class members, setting a default value to parameters, and function return types.

How types are inferred in different cases

We have already seen how types are inferred for assignments. Therefore, we’ll see a few other cases and how types are inferred in that case. Function parameters and return types work similarly to the assignments.

Expressions

When the value of a variable is derived from expression, the type is inferred from the result of that expression when it is executed.

Objects

Objects are complex data types by nature. So, it is a little different than how other values are treated. For instance, the object will not be inferred to a common type. But, each property in the object will be typed according to their values.

In the above code block, the name property will be of string and the year will be of number type respective to their values.

ALSO READ: Functional Programming in Typescript

Arrays

The array is another interesting data type that infers the type from the assigned values.

But what if an array has a few different types of elements in it? How does the typescript compiler know which data type the array belongs to? So, it enters the best common type.

Best Common Type

When a variable consists of different types of elements, the typescript compiler tries to calculate the best common type for that variable based on the different values.

In the above code block, the best common type is calculated from the values in that array. So, trying to assign any other type to that will result in a type error. This is helpful when you are working with complex data types such as arrays or objects where different types of elements are not rare.

Is there a case when the typescript compiler can not arrive at a common type or can’t figure out a type from its value?

Yes, that’s where ImplicityAny comes in.

ImplicitAny

When typescript can’t calculate the type of a variable, it silently assigns “any” as the type to it. A good example of this is,

Typescript can’t figure out whether the arguments will be numbers or strings as + is a valid operation on both. In that case, it’ll just assign “any” to both the arguments and to the return type of this function. But, using “any” as the type is as good as not using typescript at all. So, it is not what we want to happen in most cases. 

We can stop typescript from doing this and throw an error when it can’t figure out the type on its own. In your tsconfig.json  file, there is an option that lets you tweak to control this behaviour. It is noImplicitAny: true. If you set this in your config file, typescript will not assign “any” when it can’t figure out the type of a variable.

This is how type inference works in typescript. You can use this to your advantage and assign types only when it is necessary. Let the typescript compiler do the heavy lifting for you. But for me, I would rather choose to define the type of each variable on my own because I always like to keep my codebase uniform across different areas.

Get in touch with Agira technologies to hire the best web developers in the industry for building a state-of-the-art web and mobile applications for your business.

Looking for a Tech partner to dominate the digital world?
The following two tabs change content below.
Vignesh M

Vignesh M

An energetic Fullstack developer/Tech Lead and has a strong desire in Programming. With 6.5 years of experience in Angular, Javascript, Laravel PHP, Ionic, and Flutter, he always strives to come up with Smart Solutions to accomplish complex tasks.He is also interested in Video games, Motorcycles, Books, Movies, History.

Leave a Reply

Your email address will not be published. Required fields are marked *

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.