typescript - Getting a number from KO observable -
(i sorry many typescript questions - getting started ts)
i have ko observable called index. want increase value of one.
original js code:
data.index(data.index()+1);
this gives compile error: left hand side must number... value number.
var currentindex: number = data.index(); data.index(currentindex-1);
however, compile error: type '{}' not assignable type 'number'.
how tell ts index() returning number?
a knockout observable containing number
can defined so:
var myobservable : knockoutobservable<number>;
you should specify type observable contain. example:
var data = { index : ko.observable<number>(), // etc.. };
that way when call data.index()
compiler knows returned type number:
data.index(data.index() + 1); // ok
Comments
Post a Comment