I see this example in the Typescript handbook:
interface Counter {
(start: number): string;
interval: number;
reset(): void;
}
var c: Counter;
c(10);
c.reset();
c.interval = 5.0;
But when I try to do c(10);
or set c.interval = 5.0
I get an error -
Cannot set property 'interval' of undefined
I know I can do:
var c: Counter;
c = function(s: number){
return "'" + s + "'";
}
What's missing - (or is this an incomplete example) ?
Update:
There is a similar question - which answers this question, although, I still find this example convoluted.
To complete the example from the Typescript handbook:
interface Counter {
(start: number): string;
interval: number;
reset(): void;
}
function createCounter():Counter{
var counter = <Counter>function(start:number){};
counter.interval = 123;
counter.reset = function(){};
return counter;
}
createCounter()(10);
createCounter().reset();
or
var getCounter = createCounter();
getCounter(10);
getCounter.reset();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With