How to be strongly typed without specifying the type

TypeScript can have a type specified explicitly or you can have the type determined by TypeScript. The latter is called an implicit type or a type defined by inference. The action of inference is conducted by TypeScript depending on how a variable is initialized during declaration for the variable or what is returned for a function return type.

A variable inference is only possible when a value is assigned at the declaration. It means that you must set a value when using var/let/const:

const x = 1;
let y = 1;
let z;
// ...
z = 1;

In the previous code example, the value 1 is assigned to the variables x and y during the initialization. This is valid even if the colon is not used. TypeScript will infer the type for both variables. In the case of not specifying a value, only var or let would compile because it allows in a future moment the assignation. The value is not specified, which means that the type is falling back to any. This is true even if the value is set within the scope of the variable life.

In the previous code example, the value 1 was assigned to a constant and a variable. The type of both of these declarators is different. The constant type is not a number, it is a number literal of 1. It means that the type is 1 and only 1 and not any other number. However, the type of the variable declared with let is number. The reason is that with the constant, TypeScript knows that it can only be initialized once and that the value cannot change. It scopes down the simplest type it can find which is the value of the primitive. On the contrary, the variable declared with let can change its value during the lifetime of the variable. The TypeScript scopes the type every number. This is true with a number, string, and boolean:

const d1 = new Date();
let d2 = new Date();

const b1 = true;
let b2 = false;

const c1 = {
m1: 1
};

let c2 = {
m1: 1
};

However, a date will stay to the date type regardless of the declarator, the same for any class or interface because it is the smaller denominator. In the previous code example, both c1 and c2 are of the type of an object that must have a member named m1 of a type number. This example illustrates how TypeScript can also infer type inside type. m1 is a number by inference.

Inference works with functions as well. However, it has some limitations. The first one is that parameter must be explicit. The reason is that you cannot infer by usage without having a potential room for error. In the following code, the argument a is implicit any:

function f1(a) {
return a;
}

However, the return can be implicit. By returning a known type, the return type can be defined:

function f2(a: number) {
return a;
}

In the case of returning several values, TypeScript creates a union of all potential types. In the following code example, there are two return statements. TypeScript looks for each value returned and concludes that two different values are returned. The return type generated is number | string:

function f3() {
if (true) {
return 1;
} else {
return "1";
}
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.4.154