Types are spooky! (How types work?)
Types live in a separate world set apart from the "concrete variables" world. Think of it as the "upside-down" of types.
If you try to declare both a concrete variable and a type with the same name, they won't clash, since they live in separate worlds.
Types are declared by either the type
or the interface
statements. While those constructs may have peculiarities in syntax, just consider they are just ways to declare types. In the end a type will just represent some structure, regardless of which of the 2 statements you used to declare it*.
Types are immutable
You can't ever modify a type, but you can always create a new type based on another existing one;
A purpose in life
The final purpose of a type is to be linked to a concrete "living" variable, so its sins can be checked by the compiler.
What if I don't assign a type to a variable?
Every variable will always have a type. If I don't explicitly assign a type, the compiler will then infer one from the initial assignment; On VSCode, one can easily check the type of anything by mouse-overing.
An important advice about working with typescript: **mouseover everything**. Every variable. Every time. Extensively.
Seriously, you can do a big bit of "debugging" just by carefully inspecting every variable's inferred type.
A lifelong link
One variable can only have one type during its whole lifespan. However, you can still create new variables and do casts;
Going the other way
The inverse operation -- retrieving a type from a variable -- is possible with the
typeof
statement.type StrangeCat = typeof barkingFatCat
.
Last updated