/typescript/

TypeScript part 26 (interface)

2017-03-05 00:28:08

TypeScript

interface Person {
    name: string;
    age: number;
}

function printPersonName(person: Person) {
    console.log(person.name);
}

function printPersonAge(person: Person) {
    console.log(person.age);
}

let michal: Person = {
    name: "Michał",
    age: 18
};

printPersonName(michal);
printPersonAge(michal);

JavaScript

function printPersonName(person) {
    console.log(person.name);
}
function printPersonAge(person) {
    console.log(person.age);
}
var michal = {
    name: "Michał",
    age: 18
};
printPersonName(michal);
printPersonAge(michal);

TypeScript

interface Person {
    name: string;
    age: number;
    [key:string]: any;
}

let michal: Person = {
    name: "Michał",
    age: 19,
    type: "dev"
};

console.log(michal);

JavaScript

var michal = {
    name: "Michał",
    age: 19,
    type: "dev"
};
console.log(michal);

TypeScript

interface Person3 {
    name: string;
    age: number;
    introduceYourself(): void
}

let michal3: Person3 = {
    name: "Michał",
    age: 19,
    introduceYourself() {
        console.log(`My name is ${this.name}`);
    }
};

michal3.introduceYourself()

JavaScript

var michal3 = {
    name: "Michał",
    age: 19,
    introduceYourself: function () {
        console.log("My name is " + this.name);
    }
};
michal3.introduceYourself();

TypeScript

interface Person {
    name: string;
    age: number;
    introduceYourself(): void
}

class Man implements Person {
    name: string;
    age: number;

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

    introduceYourself(): void {
        console.log(`My name is ${this.name}`);
    }
}

let adam: Person = new Man("Adam", 18);

adam.introduceYourself();

JavaScript

var Man = (function () {
    function Man(name, age) {
        this.name = name;
        this.age = age;
    }
    Man.prototype.introduceYourself = function () {
        console.log("My name is " + this.name);
    };
    return Man;
}());
var adam = new Man("Adam", 18);
adam.introduceYourself();