Basics

Variables
Types


Table of Contents

In this lesson we will look at the variables in TypeScript and see the differences between TypeScript and JavaScript.

Topics on this page:


Variable Types

JavaScript has the following types (Important: The core primitive types in TypeScript are all lowercase!):

In JavaScript the above types are called dynamic since they are used at runtime.

TypeScript supports the same types as JavaScript and in addition (there are more advanced types like union and more):

TypeScript brings static types to the JavaScript language, and those types are evaluated at compile time (without having to run the code). Static types can help warn you of possible errors without having to run the code. The types have to be specified in variable declaration. Type safety provides an extra level of protection against common errors/bugs.


Variables & Declarations

Variables are created using the var, let and const keywords as in ES6 with the same scopping as in ES6.

Best practice: Use let/const instead of var whenever possible. All variable declaration statements in a function should be placed as near to the top of the function as possible. This best practice increases the clarity of the code.

TypeScript has the same naming rules as JavaScript.

TypeScript is static typed language (as C, where the type of a variable is known at compile time). Thus, variable declaration contains type annotation like:

#

We don't have to specify types absolutely everywhere in the code because TypeScript has what is called type Inference. Type inference is what the TypeScript compiler uses to automatically determine types.

In the picture above, since we assign a number value, the type can be infered automatically to determine that num is going to be of typed number. Thus, we don't have to declare it as a type number explicitely. We can declare is without type annotation exactly as in JavaScript.
However, in TypeScript, anytime you use num it's going to automatically assume that num is going to be a number and it will type check for you in the TypeScript side.

Best practice: Always use type annotation if it is not inferred.

Type annotation examples are shown below. Run the following Code in TypeScript and see for errors.

#

Arrays

Arrays are accessed and used much like in JavaScript. In TypeScript an array can be declared in two different ways:

// square bracket notation
var arr: string[] = [ 'first', 'second' ];

// angle bracket notation
var arr: Array = [ 'first', 'second' ];
        

If an array has to store different types, it has to be declared as type 'any'.


Enums

An enum is a way to associate names to a constant value, which can be either a number, a string or mixed. It is similar to C, but is more powerfull. For more information see Enums.

An example of using strings values in enum:

enum Colors1 {
    Red = 'red',
    Blue = 'lightblue',
    Green = 'green',
}

var col: Colors1  = Colors.Blue;
console.log( col );

enum Colors2 {
    Red = 1,
    Blue = 3,
    Green = 5,
}

var col: Colors2  = Colors2.Blue;
console.log( Colors2[ 5 ] );
console.log( Colors2[ col ] );
        

Tuple

Tuple types allow you to express an array with a fixed number of elements whose types are known in adavnce (fixed types). When you see a tuple versus an array, you know that you’re dealing with one piece of data entity instead of multiple pieces of data.

See Tuples.

Example:

// declare a tuple type
let person: [ string, string, number ];

// initialize it
person = [ 'Tania', 'Orenstein', 100 ]; 
console.log( person[ 0 ], person[ 1 ], person[ 2 ]);

// tuple destructuring
let [ fName, lName, grade ] = person;
console.log ( fName, lName, grade );
        

Accessing an element outside the set of known indices fails with an error.