Knockout JS Definitions for 0.9x

Topics: General
Apr 29, 2013 at 3:19 PM
Hi, has anyone put together (and would like to share :-) ) a .d.ts for Knockout JS that leverages generics?

Thanks.
Apr 29, 2013 at 4:10 PM
Hi, I have a version I have been using in one of my projects - you are welcome to give it a go.

https://gist.github.com/pgbross/5482209


It is based on the non-generic version on the DefinitelyTyped github site.

Cheers
--Philip.
Apr 29, 2013 at 6:09 PM
@Philip, thanks.

The following doesn't seem right:
            var foo = ko.observable<string>();
            foo = "bar"; // foo should be a function not a string
           // This should be string
            var baz = foo(); // TS2088 Unable to invoke type with no call signatures
Apr 29, 2013 at 10:32 PM
@nabog, you are right.

I hadn't spotted that initialising a variable using the static empty constructor, eg. ko.observable<string>(), was picking the wrong overload from KnockoutObservableStatic.

The KnockoutObservableStatic interface was there to provide access to the ".fn" prototype extension point. As I don't use it in my code, I have updated my definitions file at https://gist.github.com/pgbross/5482209 to define the static ko.observable directly.
...
//observable: KnockoutObservableStatic;
observable<T>( value?: T ): KnockoutObservable;
computed: KnockoutComputedStatic;
observableArray<T>( value?: T[] ): KnockoutObservableArray;
...
Hopefully that will help the behaviour for you, but if you need access to the ".fn" extension point that would no longer work I guess. I don't know if there is a better way of defining the interface that would work.

cheers,
--philip