Typescript. Hello world

install typescript with

 npm install -g typescript

or update if already been installed

npm update -g typescript

Now lets create some files…

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello world, typeScript!</title>
</head>
<body>
<div id="person"></div>
<script src="index.js"></script>
</body>
</html>

index.ts

class Person {
    name: string;

    constructor(name: string) {
        this.name = name;
    }
}

const stanley: Person = new Person("Stanley");
const header = this.document.getElementById("person");
header.innerHTML = "Hello" + stanley.name;

in terminal lets compile our ts file

tsc index.ts

and we will receive index.js file in our directory

Contents of this file will be the following

var Person = /** @class */ (function () {
    function Person(name) {
        this.name = name;
    }
    return Person;
}());
var stanley = new Person("Stanley");
var header = this.document.getElementById("person");
header.innerHTML = "Hello " + stanley.name;

now lets open our index.js file in browser

This entry was posted in Без рубрики. Bookmark the permalink.