UNPKG

44 kBTypeScriptView Raw
1// Type definitions for Knockout v3.4.0
2// Project: http://knockoutjs.com
3// Definitions by: Boris Yankov <https://github.com/borisyankov>,
4// Igor Oleinikov <https://github.com/Igorbek>,
5// Clément Bourgeois <https://github.com/moonpyk>,
6// Matt Brooks <https://github.com/EnableSoftware>,
7// Benjamin Eckardt <https://github.com/BenjaminEckardt>,
8// Mathias Lorenzen <https://github.com/ffMathy>,
9// Leonardo Lombardi <https://github.com/ltlombardi>
10// Retsam <https://github.com/Retsam>
11// Rey Pena <https://github.com/ReyPena>
12// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
13
14interface KnockoutSubscribableFunctions<T> {
15 /**
16 * Notify subscribers of knockout "change" event. This doesn't actually change the observable value.
17 * @param eventValue A value to be sent with the event.
18 * @param event The knockout event.
19 */
20 notifySubscribers(eventValue?: T, event?: "change"): void;
21 /**
22 * Notify subscribers of a knockout or user defined event.
23 * @param eventValue A value to be sent with the event.
24 * @param event The knockout or user defined event name.
25 */
26 notifySubscribers<U>(eventValue: U, event: string): void;
27}
28
29interface KnockoutComputedFunctions<T> {
30}
31
32interface KnockoutObservableFunctions<T> {
33 /**
34 * Used by knockout to decide if value of observable has changed and should notify subscribers. Returns true if instances are primitives, and false if are objects.
35 * If your observable holds an object, this can be overwritten to return equality based on your needs.
36 * @param a previous value.
37 * @param b next value.
38 */
39 equalityComparer(a: T, b: T): boolean;
40}
41
42// The functions of observable arrays that don't mutate the array
43interface KnockoutReadonlyObservableArrayFunctions<T> {
44 /**
45 * Returns the index of the first occurrence of a value in an array.
46 * @param searchElement The value to locate in the array.
47 * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
48 */
49 indexOf(searchElement: T, fromIndex?: number): number;
50 /**
51 * Returns a section of an array.
52 * @param start The beginning of the specified portion of the array.
53 * @param end The end of the specified portion of the array.
54 */
55 slice(start: number, end?: number): T[];
56}
57// The functions of observable arrays that mutate the array
58interface KnockoutObservableArrayFunctions<T> extends KnockoutReadonlyObservableArrayFunctions<T> {
59 /**
60 * Removes and returns all the remaining elements starting from a given index.
61 * @param start The zero-based location in the array from which to start removing elements.
62 */
63 splice(start: number): T[];
64 /**
65 * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
66 * @param start The zero-based location in the array from which to start removing elements.
67 * @param deleteCount The number of elements to remove.
68 * @param items Elements to insert into the array in place of the deleted elements.
69 */
70 splice(start: number, deleteCount: number, ...items: T[]): T[];
71 /**
72 * Removes the last value from the array and returns it.
73 */
74 pop(): T;
75 /**
76 * Adds new item or items to the end of array.
77 * @param items Items to be added.
78 */
79 push(...items: T[]): void;
80 /**
81 * Removes the first value from the array and returns it.
82 */
83 shift(): T;
84 /**
85 * Inserts new item or items at the beginning of the array.
86 * @param items Items to be added.
87 */
88 unshift(...items: T[]): number;
89 /**
90 * Reverses the order of the array and returns the observableArray (not the underlying array).
91 */
92 reverse(): KnockoutObservableArray<T>;
93 /**
94 * Sorts the array contents and returns the observableArray.
95 */
96 sort(): KnockoutObservableArray<T>;
97 /**
98 * Sorts the array contents and returns the observableArray.
99 * @param compareFunction A function that returns negative value if first argument is smaller, positive value if second is smaller, or zero to treat them as equal.
100 */
101 sort(compareFunction: (left: T, right: T) => number): KnockoutObservableArray<T>;
102
103 // Ko specific
104 /**
105 * Replaces the first value that equals oldItem with newItem.
106 * @param oldItem Item to be replaced.
107 * @param newItem Replacing item.
108 */
109 replace(oldItem: T, newItem: T): void;
110 /**
111 * Removes all values that equal item and returns them as an array.
112 * @param item The item to be removed.
113 */
114 remove(item: T): T[];
115 /**
116 * Removes all values and returns them as an array.
117 * @param removeFunction A function used to determine true if item should be removed and fasle otherwise.
118 */
119 remove(removeFunction: (item: T) => boolean): T[];
120 /**
121 * Removes all values that equal any of the supplied items.
122 * @param items Items to be removed.
123 */
124 removeAll(items: T[]): T[];
125 /**
126 * Removes all values and returns them as an array.
127 */
128 removeAll(): T[];
129
130 // Ko specific Usually relevant to Ruby on Rails developers only
131 /**
132 * Finds any objects in the array that equal someItem and gives them a special property called _destroy with value true.
133 * @param item Items to be marked with the property.
134 */
135 destroy(item: T): void;
136 /**
137 * Finds any objects in the array filtered by a function and gives them a special property called _destroy with value true.
138 * @param destroyFunction A function used to determine which items should be marked with the property.
139 */
140 destroy(destroyFunction: (item: T) => boolean): void;
141 /**
142 * Finds any objects in the array that equal suplied items and gives them a special property called _destroy with value true.
143 * @param items
144 */
145 destroyAll(items: T[]): void;
146 /**
147 * Gives a special property called _destroy with value true to all objects in the array.
148 */
149 destroyAll(): void;
150}
151
152interface KnockoutSubscribableStatic {
153 fn: KnockoutSubscribableFunctions<any>;
154
155 new <T>(): KnockoutSubscribable<T>;
156}
157
158interface KnockoutSubscription {
159 /**
160 * Terminates a subscription.
161 */
162 dispose(): void;
163}
164
165interface KnockoutSubscribable<T> extends KnockoutSubscribableFunctions<T> {
166 /**
167 * Registers to be notified after the observable's value changes.
168 * @param callback Function that is called whenever the notification happens.
169 * @param target Defines the value of 'this' in the callback function.
170 * @param event The knockout event name.
171 */
172 subscribe(callback: (newValue: T) => void, target?: any, event?: "change"): KnockoutSubscription;
173 /**
174 * Registers to be notified before the observable's value changes.
175 * @param callback Function that is called whenever the notification happens.
176 * @param target Defines the value of 'this' in the callback function.
177 * @param event The knockout event name.
178 */
179 subscribe(callback: (newValue: T) => void, target: any, event: "beforeChange"): KnockoutSubscription;
180 /**
181 * Registers to be notified when a knockout or user defined event happens.
182 * @param callback Function that is called whenever the notification happens. eventValue can be anything. No relation to underlying observable.
183 * @param target Defines the value of 'this' in the callback function.
184 * @param event The knockout or user defined event name.
185 */
186 subscribe<U>(callback: (eventValue: U) => void, target: any, event: string): KnockoutSubscription;
187 /**
188 * Customizes observables basic functionality.
189 * @param requestedExtenders Name of the extender feature and its value, e.g. { notify: 'always' }, { rateLimit: 50 }
190 */
191 extend(requestedExtenders: { [key: string]: any; }): KnockoutSubscribable<T>;
192 /**
193 * Gets total number of subscribers.
194 */
195 getSubscriptionsCount(): number;
196 /**
197 * Gets number of subscribers of a particular event.
198 * @param event Event name.
199 */
200 getSubscriptionsCount(event: string): number;
201}
202
203interface KnockoutComputedStatic {
204 fn: KnockoutComputedFunctions<any>;
205
206 /**
207 * Creates computed observable.
208 */
209 <T>(): KnockoutComputed<T>;
210 /**
211 * Creates computed observable.
212 * @param evaluatorFunction Function that computes the observable value.
213 * @param context Defines the value of 'this' when evaluating the computed observable.
214 * @param options An object with further properties for the computed observable.
215 */
216 <T>(evaluatorFunction: () => T, context?: any, options?: KnockoutComputedOptions<T>): KnockoutComputed<T>;
217 /**
218 * Creates computed observable.
219 * @param options An object that defines the computed observable options and behavior.
220 * @param context Defines the value of 'this' when evaluating the computed observable.
221 */
222 <T>(options: KnockoutComputedDefine<T>, context?: any): KnockoutComputed<T>;
223}
224
225interface KnockoutReadonlyComputed<T> extends KnockoutReadonlyObservable<T> {
226 /**
227 * Returns whether the computed observable may be updated in the future. A computed observable is inactive if it has no dependencies.
228 */
229 isActive(): boolean;
230 /**
231 * Returns the current number of dependencies of the computed observable.
232 */
233 getDependenciesCount(): number;
234}
235
236interface KnockoutComputed<T> extends KnockoutReadonlyComputed<T>, KnockoutObservable<T>, KnockoutComputedFunctions<T> {
237 fn: KnockoutComputedFunctions<any>;
238
239 /**
240 * Manually disposes the computed observable, clearing all subscriptions to dependencies.
241 * This function is useful if you want to stop a computed observable from being updated or want to clean up memory for a
242 * computed observable that has dependencies on observables that wont be cleaned.
243 */
244 dispose(): void;
245 /**
246 * Customizes observables basic functionality.
247 * @param requestedExtenders Name of the extender feature and it's value, e.g. { notify: 'always' }, { rateLimit: 50 }
248 */
249 extend(requestedExtenders: { [key: string]: any; }): KnockoutComputed<T>;
250}
251
252interface KnockoutObservableArrayStatic {
253 fn: KnockoutObservableArrayFunctions<any>;
254
255 <T>(value?: T[] | null): KnockoutObservableArray<T>;
256}
257
258/**
259 * While all observable arrays are writable at runtime, this type is analogous to the native ReadonlyArray type:
260 * casting an observable array to this type expresses the intention that it shouldn't be mutated.
261 */
262interface KnockoutReadonlyObservableArray<T> extends KnockoutReadonlyObservable<ReadonlyArray<T>>, KnockoutReadonlyObservableArrayFunctions<T> {
263 // NOTE: Keep in sync with KnockoutObservableArray<T>, see note on KnockoutObservableArray<T>
264 subscribe(callback: (newValue: KnockoutArrayChange<T>[]) => void, target: any, event: "arrayChange"): KnockoutSubscription;
265 subscribe(callback: (newValue: T[]) => void, target: any, event: "beforeChange"): KnockoutSubscription;
266 subscribe(callback: (newValue: T[]) => void, target?: any, event?: "change"): KnockoutSubscription;
267 subscribe<U>(callback: (newValue: U) => void, target: any, event: string): KnockoutSubscription;
268}
269
270/*
271 NOTE: In theory this should extend both KnockoutObservable<T[]> and KnockoutReadonlyObservableArray<T>,
272 but can't since they both provide conflicting typings of .subscribe.
273 So it extends KnockoutObservable<T[]> and duplicates the subscribe definitions, which should be kept in sync
274*/
275interface KnockoutObservableArray<T> extends KnockoutObservable<T[]>, KnockoutObservableArrayFunctions<T> {
276 subscribe(callback: (newValue: KnockoutArrayChange<T>[]) => void, target: any, event: "arrayChange"): KnockoutSubscription;
277 subscribe(callback: (newValue: T[]) => void, target: any, event: "beforeChange"): KnockoutSubscription;
278 subscribe(callback: (newValue: T[]) => void, target?: any, event?: "change"): KnockoutSubscription;
279 subscribe<U>(callback: (newValue: U) => void, target: any, event: string): KnockoutSubscription;
280
281 extend(requestedExtenders: { [key: string]: any; }): KnockoutObservableArray<T>;
282}
283
284interface KnockoutObservableStatic {
285 fn: KnockoutObservableFunctions<any>;
286
287 <T>(value: T): KnockoutObservable<T>;
288 <T>(value: any): KnockoutObservable<T>;
289 <T = any>(value: null): KnockoutObservable<T | null>
290 <T = any>(): KnockoutObservable<T | undefined>
291}
292
293/**
294 * While all observable are writable at runtime, this type is analogous to the native ReadonlyArray type:
295 * casting an observable to this type expresses the intention that this observable shouldn't be mutated.
296 */
297interface KnockoutReadonlyObservable<T> extends KnockoutSubscribable<T>, KnockoutObservableFunctions<T> {
298 (): T;
299
300 /**
301 * Returns the current value of the computed observable without creating a dependency.
302 */
303 peek(): T;
304 valueHasMutated?: { (): void; } | undefined;
305 valueWillMutate?: { (): void; } | undefined;
306}
307
308interface KnockoutObservable<T> extends KnockoutReadonlyObservable<T> {
309 (value: T): void;
310
311 // Since .extend does arbitrary thing to an observable, it's not safe to do on a readonly observable
312 /**
313 * Customizes observables basic functionality.
314 * @param requestedExtenders Name of the extender feature and it's value, e.g. { notify: 'always' }, { rateLimit: 50 }
315 */
316 extend(requestedExtenders: { [key: string]: any; }): KnockoutObservable<T>;
317}
318
319interface KnockoutComputedOptions<T> {
320 /**
321 * Makes the computed observable writable. This is a function that receives values that other code is trying to write to your computed observable.
322 * Its up to you to supply custom logic to handle the incoming values, typically by writing the values to some underlying observable(s).
323 * @param value Value being written to the computer observable.
324 */
325 write?(value: T): void;
326 /**
327 * Disposal of the computed observable will be triggered when the specified DOM node is removed by KO.
328 * This feature is used to dispose computed observables used in bindings when nodes are removed by the template and control-flow bindings.
329 */
330 disposeWhenNodeIsRemoved?: Node | undefined;
331 /**
332 * This function is executed before each re-evaluation to determine if the computed observable should be disposed.
333 * A true-ish result will trigger disposal of the computed observable.
334 */
335 disposeWhen?(): boolean;
336 /**
337 * Defines the value of 'this' whenever KO invokes your 'read' or 'write' callbacks.
338 */
339 owner?: any;
340 /**
341 * If true, then the value of the computed observable will not be evaluated until something actually attempts to access its value or manually subscribes to it.
342 * By default, a computed observable has its value determined immediately during creation.
343 */
344 deferEvaluation?: boolean | undefined;
345 /**
346 * If true, the computed observable will be set up as a purecomputed observable. This option is an alternative to the ko.pureComputed constructor.
347 */
348 pure?: boolean | undefined;
349}
350
351interface KnockoutComputedDefine<T> extends KnockoutComputedOptions<T> {
352 /**
353 * A function that is used to evaluate the computed observables current value.
354 */
355 read(): T;
356}
357
358interface KnockoutBindingContext {
359 $parent: any;
360 $parents: any[];
361 $root: any;
362 $data: any;
363 $rawData: any | KnockoutObservable<any>;
364 $index?: KnockoutObservable<number> | undefined;
365 $parentContext?: KnockoutBindingContext | undefined;
366 $component: any;
367 $componentTemplateNodes: Node[];
368
369 /**
370 * Clones the current Binding Context, adding extra properties to it.
371 * @param properties object with properties to be added in the binding context.
372 */
373 extend(properties: { [key: string]: any; } | (() => { [key: string]: any; })): KnockoutBindingContext;
374 /**
375 * This returns a new binding context whose viewmodel is the first parameter and whose $parentContext is the current bindingContext.
376 * @param dataItemOrAccessor The binding context of the children.
377 * @param dataItemAlias An alias for the data item in descendant contexts.
378 * @param extendCallback Function to be called.
379 * @param options Further options.
380 */
381 createChildContext(dataItemOrAccessor: any, dataItemAlias?: string, extendCallback?: Function, options?: { "exportDependencies": boolean }): any;
382}
383
384interface KnockoutAllBindingsAccessor {
385 (): any;
386 get(name: string): any;
387 has(name: string): boolean;
388}
389
390interface KnockoutBindingHandler<E extends Node = any, V = any, VM = any> {
391 after?: Array<string> | undefined;
392 init?: ((element: E, valueAccessor: () => V, allBindingsAccessor: KnockoutAllBindingsAccessor, viewModel: VM, bindingContext: KnockoutBindingContext) => void | { controlsDescendantBindings: boolean; }) | undefined;
393 update?: ((element: E, valueAccessor: () => V, allBindingsAccessor: KnockoutAllBindingsAccessor, viewModel: VM, bindingContext: KnockoutBindingContext) => void) | undefined;
394 options?: any;
395 preprocess?: ((value: string, name: string, addBindingCallback?: (name: string, value: string) => void) => string) | undefined;
396 [s: string]: any;
397}
398
399interface KnockoutBindingHandlers {
400 [bindingHandler: string]: KnockoutBindingHandler;
401
402 // Controlling text and appearance
403 visible: KnockoutBindingHandler;
404 text: KnockoutBindingHandler;
405 html: KnockoutBindingHandler;
406 css: KnockoutBindingHandler;
407 style: KnockoutBindingHandler;
408 attr: KnockoutBindingHandler;
409
410 // Control Flow
411 foreach: KnockoutBindingHandler;
412 if: KnockoutBindingHandler;
413 ifnot: KnockoutBindingHandler;
414 with: KnockoutBindingHandler;
415
416 // Working with form fields
417 click: KnockoutBindingHandler;
418 event: KnockoutBindingHandler;
419 submit: KnockoutBindingHandler;
420 enable: KnockoutBindingHandler;
421 disable: KnockoutBindingHandler;
422 value: KnockoutBindingHandler;
423 textInput: KnockoutBindingHandler;
424 hasfocus: KnockoutBindingHandler;
425 checked: KnockoutBindingHandler;
426 options: KnockoutBindingHandler;
427 selectedOptions: KnockoutBindingHandler;
428 uniqueName: KnockoutBindingHandler;
429
430 // Rendering templates
431 template: KnockoutBindingHandler;
432
433 // Components (new for v3.2)
434 component: KnockoutBindingHandler;
435}
436
437interface KnockoutMemoization {
438 memoize(callback: Function): string;
439 unmemoize(memoId: string, callbackParams: any[]): boolean;
440 unmemoizeDomNodeAndDescendants(domNode: any, extraCallbackParamsArray: any[]): boolean;
441 parseMemoText(memoText: string): string;
442}
443
444interface KnockoutVirtualElement { }
445
446interface KnockoutVirtualElements {
447 allowedBindings: { [bindingName: string]: boolean; };
448 emptyNode(node: KnockoutVirtualElement): void;
449 firstChild(node: KnockoutVirtualElement): KnockoutVirtualElement;
450 insertAfter(container: KnockoutVirtualElement, nodeToInsert: Node, insertAfter: Node): void;
451 nextSibling(node: KnockoutVirtualElement): Node;
452 prepend(node: KnockoutVirtualElement, toInsert: Node): void;
453 setDomNodeChildren(node: KnockoutVirtualElement, newChildren: { length: number;[index: number]: Node; }): void;
454 childNodes(node: KnockoutVirtualElement): Node[];
455}
456
457interface KnockoutExtenders {
458 throttle(target: any, timeout: number): KnockoutComputed<any>;
459 notify(target: any, notifyWhen: string): any;
460
461 rateLimit(target: any, timeout: number): any;
462 rateLimit(target: any, options: { timeout: number; method?: string | undefined; }): any;
463
464 trackArrayChanges(target: any): any;
465}
466
467//
468// NOTE TO MAINTAINERS AND CONTRIBUTORS : pay attention to only include symbols that are
469// publicly exported in the minified version of ko, without that you can give the false
470// impression that some functions will be available in production builds.
471//
472interface KnockoutUtils {
473 //////////////////////////////////
474 // utils.domData.js
475 //////////////////////////////////
476
477 domData: {
478 get(node: Node, key: string): any;
479
480 set(node: Node, key: string, value: any): void;
481
482 getAll(node: Node, createIfNotFound: boolean): any;
483
484 clear(node: Node): boolean;
485 };
486
487 //////////////////////////////////
488 // utils.domNodeDisposal.js
489 //////////////////////////////////
490
491 domNodeDisposal: {
492 addDisposeCallback(node: Node, callback: Function): void;
493
494 removeDisposeCallback(node: Node, callback: Function): void;
495
496 cleanNode(node: Node): Node;
497
498 removeNode(node: Node): void;
499 };
500
501 addOrRemoveItem<T>(array: T[] | KnockoutObservable<T>, value: T, included: T): void;
502
503 arrayFilter<T>(array: T[], predicate: (item: T) => boolean): T[];
504
505 arrayFirst<T>(array: T[], predicate: (item: T) => boolean, predicateOwner?: any): T;
506
507 arrayForEach<T>(array: T[], action: (item: T, index: number) => void): void;
508
509 arrayGetDistinctValues<T>(array: T[]): T[];
510
511 arrayIndexOf<T>(array: T[], item: T): number;
512
513 arrayMap<T, U>(array: T[], mapping: (item: T) => U): U[];
514
515 arrayPushAll<T>(array: T[] | KnockoutObservableArray<T>, valuesToPush: T[]): T[];
516
517 arrayRemoveItem(array: any[], itemToRemove: any): void;
518
519 compareArrays<T>(a: T[], b: T[]): Array<KnockoutArrayChange<T>>;
520
521 extend(target: Object, source: Object): Object;
522
523 fieldsIncludedWithJsonPost: any[];
524
525 getFormFields(form: any, fieldName: string): any[];
526
527 objectForEach(obj: any, action: (key: any, value: any) => void): void;
528
529 parseHtmlFragment(html: string): any[];
530
531 parseJson(jsonString: string): any;
532
533 postJson(urlOrForm: any, data: any, options: any): void;
534
535 peekObservable<T>(value: KnockoutObservable<T> | T): T;
536
537 range(min: any, max: any): any;
538
539 registerEventHandler(element: any, eventType: any, handler: Function): void;
540
541 setHtml(node: Element, html: () => string): void;
542
543 setHtml(node: Element, html: string): void;
544
545 setTextContent(element: any, textContent: string | KnockoutObservable<string>): void;
546
547 stringifyJson(data: any, replacer?: Function, space?: string): string;
548
549 toggleDomNodeCssClass(node: any, className: string, shouldHaveClass: boolean): void;
550
551 triggerEvent(element: any, eventType: any): void;
552
553 unwrapObservable<T>(value: KnockoutObservable<T> | T): T;
554 unwrapObservable<T>(value: KnockoutObservableArray<T> | T[]): T[];
555
556 // NOT PART OF THE MINIFIED API SURFACE (ONLY IN knockout-{version}.debug.js) https://github.com/SteveSanderson/knockout/issues/670
557 // forceRefresh(node: any): void;
558 // ieVersion: number;
559 // isIe6: boolean;
560 // isIe7: boolean;
561 // jQueryHtmlParse(html: string): any[];
562 // makeArray(arrayLikeObject: any): any[];
563 // moveCleanedNodesToContainerElement(nodes: any[]): HTMLElement;
564 // replaceDomNodes(nodeToReplaceOrNodeArray: any, newNodesArray: any[]): void;
565 // setDomNodeChildren(domNode: any, childNodes: any[]): void;
566 // setElementName(element: any, name: string): void;
567 // setOptionNodeSelectionState(optionNode: any, isSelected: boolean): void;
568 // simpleHtmlParse(html: string): any[];
569 // stringStartsWith(str: string, startsWith: string): boolean;
570 // stringTokenize(str: string, delimiter: string): string[];
571 // stringTrim(str: string): string;
572 // tagNameLower(element: any): string;
573}
574
575interface KnockoutArrayChange<T> {
576 status: "added" | "deleted" | "retained";
577 value: T;
578 index: number;
579 moved?: number | undefined;
580}
581
582//////////////////////////////////
583// templateSources.js
584//////////////////////////////////
585
586interface KnockoutTemplateSourcesDomElement {
587 text(): any;
588 text(value: any): void;
589
590 data(key: string): any;
591 data(key: string, value: any): any;
592}
593
594interface KnockoutTemplateAnonymous extends KnockoutTemplateSourcesDomElement {
595 nodes(): any;
596 nodes(value: any): void;
597}
598
599interface KnockoutTemplateSources {
600
601 domElement: {
602 prototype: KnockoutTemplateSourcesDomElement
603 new(element: Element): KnockoutTemplateSourcesDomElement
604 };
605
606 anonymousTemplate: {
607 prototype: KnockoutTemplateAnonymous;
608 new(element: Element): KnockoutTemplateAnonymous;
609 };
610}
611
612//////////////////////////////////
613// nativeTemplateEngine.js
614//////////////////////////////////
615
616interface KnockoutNativeTemplateEngine extends KnockoutTemplateEngine {
617
618 renderTemplateSource(templateSource: Object, bindingContext?: KnockoutBindingContext, options?: Object): any[];
619}
620
621//////////////////////////////////
622// templateEngine.js
623//////////////////////////////////
624
625interface KnockoutTemplateEngine {
626
627 createJavaScriptEvaluatorBlock(script: string): string;
628
629 makeTemplateSource(template: any, templateDocument?: Document): any;
630
631 renderTemplate(template: any, bindingContext: KnockoutBindingContext, options: Object, templateDocument: Document): any;
632
633 isTemplateRewritten(template: any, templateDocument: Document): boolean;
634
635 rewriteTemplate(template: any, rewriterCallback: Function, templateDocument: Document): void;
636}
637
638//////////////////////////////////
639// tasks.js
640//////////////////////////////////
641
642interface KnockoutTasks {
643 scheduler: (callback: Function) => any;
644 schedule(task: Function): number;
645 cancel(handle: number): void;
646 runEarly(): void;
647}
648
649/////////////////////////////////
650interface KnockoutStatic {
651 utils: KnockoutUtils;
652 memoization: KnockoutMemoization;
653
654 bindingHandlers: KnockoutBindingHandlers;
655 getBindingHandler(handler: string): KnockoutBindingHandler;
656
657 virtualElements: KnockoutVirtualElements;
658 extenders: KnockoutExtenders;
659
660 applyBindings(viewModelOrBindingContext?: any, rootNode?: any): void;
661 applyBindingsToDescendants(viewModelOrBindingContext: any, rootNode: any): void;
662 applyBindingAccessorsToNode(node: Node, bindings: (bindingContext: KnockoutBindingContext, node: Node) => {}, bindingContext: KnockoutBindingContext): void;
663 applyBindingAccessorsToNode(node: Node, bindings: {}, bindingContext: KnockoutBindingContext): void;
664 applyBindingAccessorsToNode(node: Node, bindings: (bindingContext: KnockoutBindingContext, node: Node) => {}, viewModel: any): void;
665 applyBindingAccessorsToNode(node: Node, bindings: {}, viewModel: any): void;
666 applyBindingsToNode(node: Node, bindings: any, viewModelOrBindingContext?: any): any;
667
668 subscribable: KnockoutSubscribableStatic;
669 observable: KnockoutObservableStatic;
670
671 computed: KnockoutComputedStatic;
672 /**
673 * Creates a pure computed observable.
674 * @param evaluatorFunction Function that computes the observable value.
675 * @param context Defines the value of 'this' when evaluating the computed observable.
676 */
677 pureComputed<T>(evaluatorFunction: () => T, context?: any): KnockoutComputed<T>;
678 /**
679 * Creates a pure computed observable.
680 * @param options An object that defines the computed observable options and behavior.
681 * @param context Defines the value of 'this' when evaluating the computed observable.
682 */
683 pureComputed<T>(options: KnockoutComputedDefine<T>, context?: any): KnockoutComputed<T>;
684
685 observableArray: KnockoutObservableArrayStatic;
686
687 /**
688 * Evaluates if instance is a KnockoutSubscribable.
689 * @param instance Instance to be evaluated.
690 */
691 isSubscribable(instance: any): instance is KnockoutSubscribable<any>;
692 /**
693 * Clones object substituting each observable for it's underlying value. Uses browser JSON.stringify internally to stringify the result.
694 * @param viewModel Object with observables to be converted.
695 * @param replacer A Function or array of names that alters the behavior of the stringification process.
696 * @param space Used to insert white space into the output JSON string for readability purposes.
697 */
698 toJSON(viewModel: any, replacer?: Function | [string | number], space?: string | number): string;
699 /**
700 * Clones object substituting for each observable the current value of that observable.
701 * @param viewModel Object with observables to be converted.
702 */
703 toJS(viewModel: any): any;
704 /**
705 * Determine if argument is an observable. Returns true for observables, observable arrays, and all computed observables.
706 * @param instance Object to be checked.
707 */
708 isObservable(instance: any): instance is KnockoutObservable<any>;
709 /**
710 * Determine if argument is an observable. Returns true for observables, observable arrays, and all computed observables.
711 * @param instance Object to be checked.
712 */
713 isObservable<T>(instance: KnockoutObservable<T> | T): instance is KnockoutObservable<T>;
714 /**
715 * Determine if argument is a writable observable. Returns true for observables, observable arrays, and writable computed observables.
716 * @param instance Object to be checked.
717 */
718 isWriteableObservable(instance: any): instance is KnockoutObservable<any>;
719 /**
720 * Determine if argument is a writable observable. Returns true for observables, observable arrays, and writable computed observables.
721 * @param instance Object to be checked.
722 */
723 isWriteableObservable<T>(instance: KnockoutObservable<T> | T): instance is KnockoutObservable<T>;
724 /**
725 * Determine if argument is a computed observable.
726 * @param instance Object to be checked.
727 */
728 isComputed(instance: any): instance is KnockoutComputed<any>;
729 /**
730 * Determine if argument is a computed observable.
731 * @param instance Object to be checked.
732 */
733 isComputed<T>(instance: KnockoutObservable<T> | T): instance is KnockoutComputed<T>;
734
735 /**
736 * Returns the data that was available for binding against the element.
737 * @param node Html node that contains the binding context.
738 */
739 dataFor(node: Node): any;
740 /**
741 * Returns the entire binding context that was available to the DOM element.
742 * @param node Html node that contains the binding context.
743 */
744 contextFor(node: Node): any;
745 /**
746 * Removes a node from the DOM.
747 * @param node Node to be removed.
748 */
749 removeNode(node: Node): void;
750 /**
751 * Used internally by Knockout to clean up data/computeds that it created related to the element. It does not remove any event handlers added by bindings.
752 * @param node Node to be cleaned.
753 */
754 cleanNode(node: Node): Node;
755 renderTemplate(template: Function, viewModel: any, options?: any, target?: any, renderMode?: any): any;
756 renderTemplate(template: string, viewModel: any, options?: any, target?: any, renderMode?: any): any;
757 /**
758 * Returns the underlying value of the Knockout Observable or in case of plain js object, return the object. Use this to easily accept both observable and plain values.
759 * @param instance observable to be unwraped if it's an Observable.
760 */
761 unwrap<T>(instance: KnockoutObservable<T> | T): T;
762 /**
763 * Gets the array inside the KnockoutObservableArray.
764 * @param instance observable to be unwraped.
765 */
766 unwrap<T>(instance: KnockoutObservableArray<T> | T[]): T[];
767
768 /**
769 * Get information about the current computed property during the execution of a computed observables evaluator function.
770 */
771 computedContext: KnockoutComputedContext;
772
773 //////////////////////////////////
774 // templateSources.js
775 //////////////////////////////////
776
777 templateSources: KnockoutTemplateSources;
778
779 //////////////////////////////////
780 // templateEngine.js
781 //////////////////////////////////
782
783 templateEngine: {
784
785 prototype: KnockoutTemplateEngine;
786
787 new(): KnockoutTemplateEngine;
788 };
789
790 //////////////////////////////////
791 // templateRewriting.js
792 //////////////////////////////////
793
794 templateRewriting: {
795
796 ensureTemplateIsRewritten(template: Node, templateEngine: KnockoutTemplateEngine, templateDocument: Document): any;
797 ensureTemplateIsRewritten(template: string, templateEngine: KnockoutTemplateEngine, templateDocument: Document): any;
798
799 memoizeBindingAttributeSyntax(htmlString: string, templateEngine: KnockoutTemplateEngine): any;
800
801 applyMemoizedBindingsToNextSibling(bindings: any, nodeName: string): string;
802 };
803
804 //////////////////////////////////
805 // nativeTemplateEngine.js
806 //////////////////////////////////
807
808 nativeTemplateEngine: {
809
810 prototype: KnockoutNativeTemplateEngine;
811
812 new(): KnockoutNativeTemplateEngine;
813
814 instance: KnockoutNativeTemplateEngine;
815 };
816
817 //////////////////////////////////
818 // jqueryTmplTemplateEngine.js
819 //////////////////////////////////
820
821 jqueryTmplTemplateEngine: {
822
823 prototype: KnockoutTemplateEngine;
824
825 renderTemplateSource(templateSource: Object, bindingContext: KnockoutBindingContext, options: Object): Node[];
826
827 createJavaScriptEvaluatorBlock(script: string): string;
828
829 addTemplate(templateName: string, templateMarkup: string): void;
830 };
831
832 //////////////////////////////////
833 // templating.js
834 //////////////////////////////////
835
836 setTemplateEngine(templateEngine: KnockoutNativeTemplateEngine | undefined): void;
837
838 renderTemplate(template: Function, dataOrBindingContext: KnockoutBindingContext, options: Object, targetNodeOrNodeArray: Node, renderMode: string): any;
839 renderTemplate(template: any, dataOrBindingContext: KnockoutBindingContext, options: Object, targetNodeOrNodeArray: Node, renderMode: string): any;
840 renderTemplate(template: Function, dataOrBindingContext: any, options: Object, targetNodeOrNodeArray: Node, renderMode: string): any;
841 renderTemplate(template: any, dataOrBindingContext: any, options: Object, targetNodeOrNodeArray: Node, renderMode: string): any;
842 renderTemplate(template: Function, dataOrBindingContext: KnockoutBindingContext, options: Object, targetNodeOrNodeArray: Node[], renderMode: string): any;
843 renderTemplate(template: any, dataOrBindingContext: KnockoutBindingContext, options: Object, targetNodeOrNodeArray: Node[], renderMode: string): any;
844 renderTemplate(template: Function, dataOrBindingContext: any, options: Object, targetNodeOrNodeArray: Node[], renderMode: string): any;
845 renderTemplate(template: any, dataOrBindingContext: any, options: Object, targetNodeOrNodeArray: Node[], renderMode: string): any;
846
847 renderTemplateForEach(template: Function, arrayOrObservableArray: any[], options: Object, targetNode: Node, parentBindingContext: KnockoutBindingContext): any;
848 renderTemplateForEach(template: any, arrayOrObservableArray: any[], options: Object, targetNode: Node, parentBindingContext: KnockoutBindingContext): any;
849 renderTemplateForEach(template: Function, arrayOrObservableArray: KnockoutObservable<any>, options: Object, targetNode: Node, parentBindingContext: KnockoutBindingContext): any;
850 renderTemplateForEach(template: any, arrayOrObservableArray: KnockoutObservable<any>, options: Object, targetNode: Node, parentBindingContext: KnockoutBindingContext): any;
851
852 /**
853 * Executes a callback function inside a computed observable, without creating a dependecy between it and the observables inside the function.
854 * @param callback Function to be called.
855 * @param callbackTarget Defines the value of 'this' in the callback function.
856 * @param callbackArgs Arguments for the callback Function.
857 */
858 ignoreDependencies<T>(callback: () => T, callbackTarget?: any, callbackArgs?: any): T;
859
860 expressionRewriting: {
861 bindingRewriteValidators: any[];
862 twoWayBindings: any;
863 parseObjectLiteral: (objectLiteralString: string) => any[];
864
865 /**
866 Internal, private KO utility for updating model properties from within bindings
867 property: If the property being updated is (or might be) an observable, pass it here
868 If it turns out to be a writable observable, it will be written to directly
869 allBindings: An object with a get method to retrieve bindings in the current execution context.
870 This will be searched for a '_ko_property_writers' property in case you're writing to a non-observable
871 (See note below)
872 key: The key identifying the property to be written. Example: for { hasFocus: myValue }, write to 'myValue' by specifying the key 'hasFocus'
873 value: The value to be written
874 checkIfDifferent: If true, and if the property being written is a writable observable, the value will only be written if
875 it is !== existing value on that writable observable
876
877 Note that if you need to write to the viewModel without an observable property,
878 you need to set ko.expressionRewriting.twoWayBindings[key] = true; *before* the binding evaluation.
879 */
880 writeValueToProperty: (property: KnockoutObservable<any> | any, allBindings: KnockoutAllBindingsAccessor, key: string, value: any, checkIfDifferent?: boolean) => void;
881 };
882
883 /////////////////////////////////
884
885 bindingProvider: {
886 instance: KnockoutBindingProvider;
887 new(): KnockoutBindingProvider;
888 }
889
890 /////////////////////////////////
891 // selectExtensions.js
892 /////////////////////////////////
893
894 selectExtensions: {
895
896 readValue(element: HTMLElement): any;
897
898 writeValue(element: HTMLElement, value: any, allowUnset?: boolean): void;
899 };
900
901 components: KnockoutComponents;
902
903 /////////////////////////////////
904 // options.js
905 /////////////////////////////////
906
907 options: {
908 deferUpdates: boolean,
909
910 useOnlyNativeEvents: boolean
911 };
912
913 /////////////////////////////////
914 // tasks.js
915 /////////////////////////////////
916
917 tasks: KnockoutTasks;
918
919 /////////////////////////////////
920 // utils.js
921 /////////////////////////////////
922
923 onError?: ((error: Error) => void) | undefined;
924}
925
926interface KnockoutBindingProvider {
927 nodeHasBindings(node: Node): boolean;
928 getBindings(node: Node, bindingContext: KnockoutBindingContext): {};
929 getBindingAccessors?(node: Node, bindingContext: KnockoutBindingContext): { [key: string]: string; };
930}
931
932interface KnockoutComputedContext {
933 /**
934 * Returns the number of dependencies of the computed observable detected so far during the current evaluation.
935 */
936 getDependenciesCount(): number;
937 /**
938 * A function that returns true if called during the first ever evaluation of the current computed observable, or false otherwise.
939 * For pure computed observables, isInitial() is always undefined.
940 */
941 isInitial: () => boolean;
942 isSleeping: boolean;
943}
944
945//
946// refactored types into a namespace to reduce global pollution
947// and used Union Types to simplify overloads (requires TypeScript 1.4)
948//
949declare namespace KnockoutComponentTypes {
950 type ViewModel = ViewModelFunction | ViewModelSharedInstance | ViewModelFactoryFunction | AMDModule;
951
952 interface Config<T> {
953 viewModel?: T | undefined;
954 template: string | Node[] | DocumentFragment | TemplateElement | AMDModule;
955 synchronous?: boolean | undefined;
956 }
957
958 interface ComponentConfig<T = ViewModel> {
959 viewModel?: T | undefined;
960 template: any;
961 createViewModel?: any;
962 }
963
964 interface EmptyConfig {
965 }
966
967 // common AMD type
968 interface AMDModule {
969 require: string;
970 }
971
972 // viewmodel types
973 interface ViewModelFunction {
974 (params?: any): any;
975 }
976
977 interface ViewModelSharedInstance {
978 instance: any;
979 }
980
981 interface ViewModelFactoryFunction {
982 createViewModel: (params: any, componentInfo: ComponentInfo) => any;
983 }
984
985 interface ComponentInfo {
986 element: Node;
987 templateNodes: Node[];
988 }
989
990 interface TemplateElement {
991 element: string | Node;
992 }
993
994 interface Loader {
995 /**
996 * Define this if: you want to supply configurations programmatically based on names, e.g., to implement a naming convention.
997 * @see {@link https://knockoutjs.com/documentation/component-loaders.html}
998 */
999 getConfig?(componentName: string, callback: (result: ComponentConfig | null) => void): void;
1000 /**
1001 * Define this if: you want to take control over how component configurations are interpreted, e.g., if you do not want to use the standard 'viewModel/template' pair format.
1002 * @see {@link https://knockoutjs.com/documentation/component-loaders.html}
1003 */
1004 loadComponent?(componentName: string, config: ComponentConfig, callback: (result: Definition | null) => void): void;
1005 /**
1006 * Define this if: you want to use custom logic to supply DOM nodes for a given template configuration (e.g., using an ajax request to fetch a template by URL).
1007 * @see {@link https://knockoutjs.com/documentation/component-loaders.html}
1008 */
1009 loadTemplate?(componentName: string, templateConfig: any, callback: (result: Node[] | null) => void): void;
1010 /**
1011 * Define this if: you want to use custom logic to supply a viewmodel factory for a given viewmodel configuration (e.g., integrating with a third-party module loader or dependency injection system).
1012 * @see {@link https://knockoutjs.com/documentation/component-loaders.html}
1013 */
1014 loadViewModel?(componentName: string, viewModelConfig: any, callback: (result: any) => void): void;
1015 suppressLoaderExceptions?: boolean | undefined;
1016 }
1017
1018 interface Definition {
1019 template: Node[];
1020 createViewModel?(params: any, options: { element: Node; }): any;
1021 }
1022}
1023
1024interface KnockoutComponents {
1025
1026 /**
1027 * Registers a component, in the default component loader, to be used by name in the component binding.
1028 * @param componentName Component name. Will be used for your custom HTML tag name.
1029 * @param config Component configuration.
1030 */
1031 register<T = KnockoutComponentTypes.ViewModel>(componentName: string, config: KnockoutComponentTypes.Config<T> | KnockoutComponentTypes.EmptyConfig): void;
1032 /**
1033 * Determine if a component with the specified name is already registered in the default component loader.
1034 * @param componentName Component name.
1035 */
1036 isRegistered(componentName: string): boolean;
1037 /**
1038 * Removes the named component from the default component loader registry. Or if no such component was registered, does nothing.
1039 * @param componentName Component name.
1040 */
1041 unregister(componentName: string): void;
1042 /**
1043 * Searchs each registered component loader by component name, and returns the viewmodel/template declaration via callback parameter.
1044 * @param componentName Component name.
1045 * @param callback Function to be called with the viewmodel/template declaration parameter.
1046 */
1047 get(componentName: string, callback: (definition: KnockoutComponentTypes.Definition) => void): void;
1048 /**
1049 * Clears the cache knockout creates to speed up component loading, for a given component.
1050 * @param componentName Component name.
1051 */
1052 clearCachedDefinition(componentName: string): void
1053 defaultLoader: KnockoutComponentTypes.Loader;
1054 loaders: KnockoutComponentTypes.Loader[];
1055 /**
1056 * Returns the registered component name for a HTML element. Can be overwriten to to control dynamically which HTML element map to which component name.
1057 * @param node html element that corresponds to a custom component.
1058 */
1059 getComponentNameForNode(node: Node): string;
1060}
1061
1062declare var ko: KnockoutStatic;
1063
1064declare module "knockout" {
1065 export = ko;
1066}
1067
\No newline at end of file