TypeScript is pure object-oriented with classes, interfaces, and statically typed like C## or Java.

TypeScript lets you write JavaScript the way you really want to. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. This article aims to teach you the basics and fundamentals of TypeScript.

Requirements

I recommend that you have a good understanding of OOP concepts and basic JavaScript before continuing on with this tutorial (so as to make the most of it).

Getting Started

Node.js is an open-source, cross-platform runtime environment for server-side JavaScript. Node.js is required to run JavaScript without browser support. Type the following command in the terminal window to install TypeScript:

$ npm install -g typescript

Typescript can be built on a plethora of development environments such as Visual Studio.

Syntax

Syntax defines a set of rules for writing programs. Every language specification defines its own syntax. An example of typescript code:

var message:string = "Hello World" 
console.log(message)

To compile your TypeScript file, enter the following command:

tsc app.ts

Then you can run it:

node app.js

Variables

A variable is like a little container to store data in. Variables are essential in TypeScript. In TypeScript, you need to specify the data type of a variable.

As in other programming languages, TypeScript contains the following data types:

  • String — A string is a sequence of characters
  • Integer — An integer data type is a non-decimal number
  • Float — A float is a number with a decimal point or a number
  • Boolean — A Boolean represents TRUE or FALSE.
  • Array — An array stores multiple values in one single variable.

Declaring & Initializing a variable

variableName:type = value;

An example:

var name:string = "Bryan";

#web-development #javascript #programming #typescript

Object-Oriented Programming with Javascript — using Typescript
1.35 GEEK