My Adventure with TypeScript (2 Part Series)
I'm not saying TypeScript it's better than those languages. Languages have different approaches on how to solve a specific problem.
Now the thing after the : or colon indicates the type of the variable.
In the above, examples are exactly the same but the two below we're just using the Generic Array type Array<someType> passing the primitive type or maybe some custom type that you made inside the less than symbol and greater than symbol.
Making Custom Type or Alias Types.
The type keyword is only available in TS, not JS. after the type keyword, we specify the name of our custom type and after the equals sign, we indicate the value of our custom type it may be a primitive type or value or another custom type. The | or pipe operator means that our Alias Type may hold one or more several types. In the first example, our StringOrNumber can only have a value of string or number. In the second example, the BoolOrNumber can only have a value of boolean or number and the third example BestGameEver can only have 3 string values.
Now let's talk about functions.
We can also specify types in parameters. Specifying types in function parameters is also the same with variables. The third parameter with the ? makes the parameter optional. We also specify the return type of the function after the last parenthesis and colon and in this case is an object.
My favorite feature in TypeScript is Interfaces.
Interfaces describe the shape of an object it tells that our object that it must have inherited properties that the interface has. We can also make an optional property by adding a ? after the property name.
Have a Nice One.