Decreasing verbosity – type inference
In this chapter, we saw the best TypeScript capabilities that help in the development of our Angular projects. We were typing all the variables and relying on the TypeScript transpiler to avoid errors that would otherwise occur in our user’s runtime.
Let’s now explore TypeScript’s powerful inference mechanisms. Through it, TypeScript identifies the types of variables by content, not requiring you to define the type explicitly. Let’s observe the following example:
export function primitive_example() { let name = "Mario"; let age = 9; let isAlive = true; console.log(`Name:${name} Age:${age} is alive:${isAlive ? "yes" : "no"}`); }
This example is the same as in Primitive and basic types, but we directly inform the values in the variables. This way of declaring the variable has the same effect as the explicit method. If you change...