TypeScript objects can be used as both functions and objects, with additional attributes.

interface Counter {
    (start: number) :string;
    interval: number;
    reset(): void;
}

function getCounter() :Counter {
    let counter = <Counter>function (start: number) {}; counter.interval =123;
    counter.reset = function () {};return counter;
}

let c = getCounter();
c(10);
c.reset();
c.interval = 5.0;
Copy the code

Corresponding generated JavaScript code:

function getCounter() {
    var counter = function (start) {}; counter.interval =123;
    counter.reset = function () {};return counter;
}
var c = getCounter();
c(10);
c.reset();
c.interval = 5.0;
Copy the code