Type Inference in typescript

Typescript infers the Data type of the variable, even if there is no type is declared. We call this Type Inference. The variables in the typescript tutorial show the various ways to declare a variable. We can declare the variable without type annotation. In such a circumstance, the Typescript makes an attempt to infer the type of the variable.

Type Inference

Typescript tries to infer the type if you do not specify one, by determining the type of the initial value assigned to it or based on its usage.

There are two ways types are inferred in Typescript. One is explicit and the other one is implicit

Explicit Typing is when we just declare the variable with the types.

In Implicit typing, we do not declare the types. Typescript makes an attempt to deduce the type from its usage.

In the above code. we declare a variable without type and but assign an initial value to it.

The Typescript infers or determines that the initial value is number, hence infers the numVar as the number.

In the next line, we try to assign a string to the numVar and the typescript compiler flags this code as an error

Example of Type Inference in Typescript
Example of Type Inference in Typescript. The compiler infers the type and throws an error right away

The above is one of the many ways in which typescript makes an attempt to infer out the type when the type information is not provided in the variable declaration.

Variable declaration

The types of variables are inferred when an initial value is assigned to it. We looked at in the previous example

If no implicit type specified or value assigned, then typescripts infers it as any.

Expressions

The type is inferred from the expression’s result.

Function Parameter Default Values

The types of function parameters are inferred, when the default values are set for parameters

Example

Function Return type

The Typescript infers the return type of function based on the return value.

Example

In the above example, the Typescript infers the return type of add function as a number. And when we assign it to the x variable, then it is also inferred as a number.

Objects

The Type inference for the complex data types like objects works in similar ways. The types of properties of the objects are infrared just like variables

The Type inference also works for complex data types like objects, arrays, etc

Example

Example

Arrays

Similarly, the data type of the array is based on its initial element assigned to it.

Example

In the following example, the array has a mixed type number and a null value. The compiler determines the type as union type of number & null. We can assign a number or null to any element of the array. The other data types result in compiler error.

Example

Example

Here is another example of a mixed type of string & number. You can assign only string and number values to the array. Any other data type results in a compiler error.

Any is the default type

What if Compiler fails to infer the type of the variable?. The Typescript assigns the type any to it.

noImplicitAny

Typescript will do its best to infer. But there are circumstances where inferring type as any may result in unexpected errors. We can make typescript throw an error whenever that happens using the "noImplicitAny": true option tsconfig.json.

Example:

Open the tsconfig.json and add the following.

In the following example, variable x & y are not annotated with any type. Hence it is unclear here, what should be the allowed values. The TypeScript infers them as any, but a compiler error results if the above flag is set to true.

Summary

Typescript inference works try to infer the type if you do not specify one, by determining the type of the initial value assigned to it.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top