ANGULAR in less than 128 words – TypeScript – Part 3

TypeScript is a very typed, object-oriented and compiled typed language. TypeScript is a typical JavaScript overset compiled in JavaScript. TypeScript is JavaScript and some additional features.

Typescript documentation is available on this link: https://www.typescriptlang.org/docs/home.html

Installing TypeScript:

sudo npm install -g typescript

Execution of the TypeScript compiler:

tsc --version

Creating a first TypeScript example:

Creating the test.ts file:

function salutation (name)
    console.log ("Hello, " - name);
}

var user name - "John";
greeting (user name);

Compilation of the code:

tsc test.ts

At the end of the order a test.js file is created that can then be viewed:

function salutation (name)
    console.log ("Hello, " - name);
}
var user name - "John";
greeting (user name);

We find that for this example the code is the same in javascript and typescript, we can run:

Node test.js
Good morning, John.

Defining a variable with TypeScript:

It is possible to declare a variable with var that is classic in javascript or with let that is more secure with TypeScript. Declaring a variable with var gives it a global scope i.e. the variable is available throughout the code, the variable’s scope limits to the « nearest block »

Example 1: use of var (global scope)

function display (test)
    var counts 1;

    if (test)
        var increase - count -5;
        return increase;
    }

    return increase;
}


display (false);

This code compiles but carries risks because the increase variable has not been declared if the test setting is false.

Example 2: use of less (scope limited to nearest block)

For the next code, the compilation is mistaken due to the limitation of the scope of the variable increase.

function display (test)
    Let counts 1;

    if (test)
        Increase - count - 5;
        return increase;
    }

    Error the variable does not exist
    return increase;
}
display (true);

test.tsc.ts
test.ts:10:12 - error TS2304: Can find name 'increase'.

TypeScript typing:

Let's count 10
'hello' account

The compilation of the code above confirms the strong typing of the language, when compiling the following error is removed:

test.tsc.ts
test.ts:2:1 - error TS2322: Type '"hello" is not assignable to type 'number'.

TypeScript type annotation:

The following types can be annotated in TypeScript:

let a: number; whole and decimal
let b: boolean;
let c: thong;
let's: any;
let's be: numbe[]r;
let f: numb[]er [4, 5, 6]'
let g: an[]y . . [4, 'Hello', true, 6].
const letterA - 'A';
Const green color - 1;

List in TypeScript:

enum Direction
    High -1,
    in Bas - 2,
    aLeft - 3,
    aRight - 4
}

next Direction - Direction.enHaut;

If we look at the code above after compilation by tsc we observe that the TypeScript code is much more comfortable than the one in JavaScript:

Var Direction;
(function (Direction)
    Directio[Direction["enHaut"]n - 1] - "high";
    Directio[Direction["enBas"]n - 2] - "Down";
    Directio[Direction["aGauche"]n - 3] - "Left";
    Directio[Direction["aDroite"]n - 4] - "right";
(Direction) (Direction));
vardirection - Direction.enHaut;

Auteur / autrice

Retour en haut