Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I made this little interface to get static types for Knockout:</p> <pre><code>interface ObservableNumber { (newValue: number): void; (): number; subscribe: (callback: (newValue: number) =&gt; void) =&gt; void; } interface ObservableString { (newValue: string): void; (): string; subscribe: (callback: (newValue: string) =&gt; void) =&gt; void; } interface ObservableBool { (newValue: bool): void; (): bool; subscribe: (callback: (newValue: bool) =&gt; void) =&gt; void; } interface ObservableAny { (newValue: any): void; (): any; subscribe: (callback: (newValue: any) =&gt; void) =&gt; void; } interface ObservableStringArray { (newValue: string[]): void; (): string[]; remove: (value: String) =&gt; void; removeAll: () =&gt; void; push: (value: string) =&gt; void; indexOf: (value: string) =&gt; number; } interface ObservableAnyArray { (newValue: any[]): void; (): any[]; remove: (value: any) =&gt; void; removeAll: () =&gt; void; push: (value: any) =&gt; void; } interface Computed { (): any; } interface Knockout { observable: { (value: number): ObservableNumber; (value: string): ObservableString; (value: bool): ObservableBool; (value: any): ObservableAny; }; observableArray: { (value: string[]): ObservableStringArray; (value: any[]): ObservableAnyArray; }; computed: { (func: () =&gt; any): Computed; }; } </code></pre> <p>Put it in "Knockout.d.ts" and then reference it from your own files. As you can see, it would benefit greatly from generics (which are coming according to the specs).</p> <p>I only made a few interfaces for ko.observable(), but ko.computed() and ko.observableArray() can be easily added in the same pattern. <strong>Update:</strong> I fixed the signatures for subscribe() and added examples of computed() and observableArray().</p> <p>To use from your own file, add this at the top:</p> <pre><code>/// &lt;reference path="./Knockout.d.ts" /&gt; declare var ko: Knockout; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload