In today’s article we are going to discuss typescript types.

So what are types ?

Why care about types ?

https://mohamedaliamaira.medium.com/what-is-typescript-and-why-it-is-inevitable-to-adapt-it-in-2021-a208bfc31c83

When To use them

Type inference and when to use types in typescript

How to use them then ?

I used a code photo so that you don’t copy it, it is important when learning new syntax to write it your own

you can use type annotation ( var name : type) with basic types like string, number (in TS numeric types are all numbers), booleans, etc…

here is a graph that contains all the types in TypeScript ( for now focus on the built in types )

in the built in types we see a new type : void

void basically is used to annotate a function ( we will get there ) and indicate that it doesn’t return anything. this is for example used when your function takes a string and just spits it in the console or slam it in the html.

looking at the right side of the graph, there is a User defined Types subtree that seems even more interesting.

So what are user defined types ?

In the code above we discussed the typing system TypeScript has to offer when it comes to objects.

in TypeScript you can describe the object and its properties like in the ‘person2’ example (check lines 13 → 16). there we see that TypeScript prevented us from adding the “blablabla_property” (check lines 22 & 23 ) at time we assign values to the variable.

now that you understood the syntax let’s continue with other types :

Arrays

Arrays are pretty simple in typescript believe me

the syntax in the photo above shows how to declare an array with a type in TypeScript, if you are going initialize the variable with a value (non empty array in this case) do not bother adding types TypeScript will do it for you.

Classes

these two Images will explain classes in TypeScript for you :

constructor difference between ES6 and TypeScript
Notice how TypeScript implements access modifiers

Final Notes

https://www.youtube.com/watch?v=rAy_3SIqT-E

https://medium.com/javascript-in-plain-english/typescript-101-understanding-the-basics-in-3-minutes-e2113a9c4c5f

a full stack web & hybride mobile developer, specialized in the mern / mevn stack, a javascript. also has experience with react native and is learning flutter