Typescript Tutorial Using types

Diana ponce
2 min readNov 15, 2020

Now it’s time to use typescript! like I mentioned in my previous blog Typescript is very easy to learn and use, mostly if you have knowledge of basic javascript, I will be giving a small tutorial using typescript.

Getting started

To being we should first go ahead to their website(https://www.typescriptlang.org/) and obtain the latest version of typescript to our computer.

W can also instal typescript using the Npm command:

npm install -g typescript

After this is finished installing we are now ready to use typescript!

Now we can go ahead and create a file using .ts at the end. For example I will name this file Example.ts and to start off we will create some variables:

let name: string = 'diana'
let age: integer=21
let likesChocolate: boolean=true
let me: string = `My name is ${name} ,I am ${age} years old
and I ${ likesChocolate : 'loveee chocolate' : 'hate chocolate' }`

So In the example we have above we have all of these variables and each of these variables contain a colon :, with a type next to it. We have name set to be a type of string, Age to be a type of integer and likesChocolate to be a boolean. Using typescript we write a bit more code but this can be useful to prevent future errors.

To run our code, since TypeScript code is not executed by javascript environments like the browsers we can run our code using this:

tsc Example.ts

in our command line, this compiles and translates the code into JavaScript which then helps browsers to read the code and is now able to be displayed.

We can also define our arrays in a couple of ways like:

let myNumberArr:numbers[] = [1,2,3,4,5]
let myStringArr:Array<String> = ['hola', 'hello', 'bonjour', 'hi']

In the next blog I will explain how we will put this all together using functions and will also continue to talk about enums and interfaces

--

--