/typescript/

TypeScript part 12 (complex object type)

2017-03-04 13:19:23

TypeScript

let person: {name: string, age: number, address: string[], dev: boolean, sayHello: () => string} = {
    name: "Michal",
    age: 18,
    address: ["Polska", "Warszawa", "Street 18"],
    dev: true,
    sayHello: function (): string {
        return "Hello"
    }
}

// person = {} <- error

// person = {name:"michal"} <-error

person = {
    name: "Szalek",
    age: 18,
    address: ["Polska", "Warszawa", "Street 18"],
    dev: true,
    sayHello: function (): string {
        return "Hello"
    }
}

JavaScript

var person = {
    name: "Michal",
    age: 18,
    address: ["Polska", "Warszawa", "Street 18"],
    dev: true,
    sayHello: function () {
        return "Hello";
    }
};
// person = {} <- error
// person = {name:"michal"} <-error
person = {
    name: "Szalek",
    age: 18,
    address: ["Polska", "Warszawa", "Street 18"],
    dev: true,
    sayHello: function () {
        return "Hello";
    }
};

TypeScript

let person: {name: string, age: number, address: string[], dev: boolean, sayHello: () => string};

// person = {} <- error

// person = {name:"michal"} <-error

person = {
    name: "Szalek",
    age: 18,
    address: ["Polska", "Warszawa", "Street 18"],
    dev: true,
    sayHello: function (): string {
        return "Hello"
    }
}

JavaScript

var person;
// person = {} <- error
// person = {name:"michal"} <-error
person = {
    name: "Szalek",
    age: 18,
    address: ["Polska", "Warszawa", "Street 18"],
    dev: true,
    sayHello: function () {
        return "Hello";
    }
};