TypeScript can infer the data type of a variable even though it has not been explicitly declared.
Type Inference
From the initialization of the variable TypeScript infers the type it will be throughout the code and this cannot vary. For example:
let myName = "Victoria";
While we do not indicate the data type as it would be done in this way:
let myName: string = "Victoria";
TypeScript infers that the variable myName
will be of type string
and henceforth cannot take a value that is not of this data type.
myName = 30;
In Visual Studio Code you can get autocompletion having suggestions according to the type of data that is the variable:

Equal variable names
TypeScript will indicate as an error those variables with the same name even though they are in different files. This will not happen in preconfigured environments such as Angular or React, since they work in a modular way or have a scope for each variable.
If you want to work with the same variable names in different files, you can create an anonymous self-executing function:
( () => { let myName = "Victoria"; })();
The same for each variable you want to have the same name(myName
for this example) you should create this type of function to avoid getting these warnings.
Contributed by: Martin Alvarez.
Want to see more contributions, questions and answers from the community?