Sergio Guerrero avatar

Simple introduction: TypeScript

chocolatoso

Published: 09 Aug 2018 › Updated: 09 Aug 2018Simple introduction: TypeScript

Simple introduction: TypeScript

“One of Ionic's main goals is to make app development as quick and easy as possible, and the tooling
support TypeScript gives us with autocompletion, type checking and source documentation really aligns with that.”
— Tim Lancina, Tooling Developer - Ionic

A little of history

TypeScript is a superset of JavaScript, which is compiled directly in javascript, adds a static typing and class-based objects. is structured, object oriented, fully functional and generic, used to develop applications both on the server side and the client side, normally uses the extension .ts

Working on

Many IDEs support TypeScript without the need for additional configuration, but TypeScript can also be compiled using Node.JS from a command line.

I personally use Visual studio, since the 2015 version includes by default TypeScript.

Installing the command line interface

Install Node.js.

Install the npm package globally

To be able to install typescript in a way that commands can be used on any directory, we must install it globally as follows

npm install -g typescript

Compiling TypeScript code

Is used to compile typescript the command tsc
for example:
tsc my-code.ts

Basic syntax && The Hello World

as typescript is a superset of JavaScript, as code written in javascript is valid. typescript adds new features making it much more strongly-typed

Type declarations

you can add type declarations to the variables, function parameters and function return types, what did you mean by this? Let's see the following:
var num: number = 5;

the compiler will check the types (when possible) during the compilation and generate the corresponding errors, for example:

var num:number* = 5;
num = "this is a string cadene"; //this generate a error: Type 'string' is not assignable to type 'number'.

you can check more types here http://www.typescriptlang.org/docs/handbook/basic-types.html

Hello World

we in typescript can define classes like this:

`class HelloWorld {
greeting: string;

      constructor(message: string) {
           this.greeting = "Hello World by: "+message;    
       }  

  greet(): string {    
     return this.greeting;    
  }

};`

We can use this class as follows

'let greeter = new Greeter ("Chocolatoso");'

'console.log (HelloWorld.greet ());' -> which prints: Hello World by: Chocolatoso

Here we have a class, HelloWorld, which has a constructor and a greeting method.
You can build an instance of that class using
the keyword new
and we will pass string so that the greeting method comes out in the console.

Leave Simple introduction: TypeScript to:

Written by

Ingeniero de sistemas. Colombia

Read more #utopian-io posts


Best Posts From Sergio Guerrero

We have not curated any of chocolatoso's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Sergio Guerrero