DEV Community

Cover image for Learn Typescript with me (Part 1)
Danish
Danish

Posted on

Learn Typescript with me (Part 1)

Lets recap basics of typescript, I know most of you already know the basics.

Basic types
string
number
boolean
any

These are basic types you will be seeing everywhere, here is example

const name: string = 'Danish' //  a string

let a: number = 23

let b: string = 'somestring'

let e: boolean = false
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

Now when you use these variables typescript will assist you in using them, see if I multiply or divide a with b it will complain, or even when I try to assign a = "Danish" it will complain

Next comes Union types

*Union types * are the mixing of the basic types like string and number, or boolean and number, basically a variable can be both a number or string.

let employeId: string | number
let isSuccess: number | boolean
Enter fullscreen mode Exit fullscreen mode

Image description

here we are assuming that employeId can be string or number, but if we assign boolean to it, typescript will complain

Infer Types, Now typescript infer types based on the value you assigned to the variable. If the value given initially to variable is false let isLoading = false now the infer type of isLoading will be boolean so typescript will complain if we try to do something like isLoading = "ok" .

Image description

Likewise if you don't know the type of some third party package you can just hover over it in your IDE and it will tell you the type.

So this was the basics recap, in next article we will see object, tuple array, optional properties, narrowing and enum.

Danish Ali.

Top comments (0)