Typescript - Hybrid Types
I see this example in the Typescript reference:
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 spacing to undefined
I know I can:
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 confusing.
+3
source to share
1 answer
To complete the example from the Typescript reference:
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();
+5
source to share