Merge pull request #17290 from MortenHoustonLudvigsen/knockout-functions

knockout: Make it possible to extend interfaces for subscribables and observables
This commit is contained in:
Ryan Cavanaugh
2017-07-13 10:18:25 -07:00
committed by GitHub
2 changed files with 84 additions and 71 deletions

View File

@@ -4,23 +4,22 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface KnockoutSubscribableFunctions<T> {
[key: string]: KnockoutBindingHandler | undefined;
notifySubscribers(valueToWrite?: T, event?: string): void;
interface KnockoutExtensionFunctions {
[key: string]: any;
}
interface KnockoutComputedFunctions<T> {
[key: string]: KnockoutBindingHandler | undefined;
interface KnockoutSubscribableFunctions<T> extends KnockoutExtensionFunctions {
notifySubscribers(valueToWrite?: T, event?: string): void;
}
interface KnockoutObservableFunctions<T> {
[key: string]: KnockoutBindingHandler | undefined;
equalityComparer(a: any, b: any): boolean;
interface KnockoutComputedFunctions<T> extends KnockoutExtensionFunctions {
}
interface KnockoutObservableArrayFunctions<T> {
interface KnockoutObservableFunctions<T> extends KnockoutExtensionFunctions {
equalityComparer(a: any, b: any): boolean;
}
interface KnockoutObservableArrayFunctions<T> extends KnockoutExtensionFunctions {
// General Array functions
indexOf(searchElement: T, fromIndex?: number): number;
slice(start: number, end?: number): T[];
@@ -35,8 +34,6 @@ interface KnockoutObservableArrayFunctions<T> {
sort(compareFunction: (left: T, right: T) => number): KnockoutObservableArray<T>;
// Ko specific
[key: string]: KnockoutBindingHandler | undefined;
replace(oldItem: T, newItem: T): void;
remove(item: T): T[];
@@ -57,16 +54,16 @@ interface KnockoutSubscribableStatic {
}
interface KnockoutSubscription {
dispose(): void;
dispose(): void;
}
interface KnockoutSubscribable<T> extends KnockoutSubscribableFunctions<T> {
subscribe(callback: (newValue: T) => void, target: any, event: "beforeChange"): KnockoutSubscription;
subscribe(callback: (newValue: T) => void, target?: any, event?: "change"): KnockoutSubscription;
subscribe<TEvent>(callback: (newValue: TEvent) => void, target: any, event: string): KnockoutSubscription;
subscribe(callback: (newValue: T) => void, target?: any, event?: "change"): KnockoutSubscription;
subscribe<TEvent>(callback: (newValue: TEvent) => void, target: any, event: string): KnockoutSubscription;
extend(requestedExtenders: { [key: string]: any; }): KnockoutSubscribable<T>;
getSubscriptionsCount(): number;
extend(requestedExtenders: { [key: string]: any; }): KnockoutSubscribable<T>;
getSubscriptionsCount(): number;
}
interface KnockoutComputedStatic {
@@ -78,11 +75,11 @@ interface KnockoutComputedStatic {
}
interface KnockoutComputed<T> extends KnockoutObservable<T>, KnockoutComputedFunctions<T> {
fn: KnockoutComputedFunctions<any>;
fn: KnockoutComputedFunctions<any>;
dispose(): void;
isActive(): boolean;
getDependenciesCount(): number;
dispose(): void;
isActive(): boolean;
getDependenciesCount(): number;
extend(requestedExtenders: { [key: string]: any; }): KnockoutComputed<T>;
}
@@ -108,23 +105,23 @@ interface KnockoutObservableStatic {
}
interface KnockoutObservable<T> extends KnockoutSubscribable<T>, KnockoutObservableFunctions<T> {
(): T;
(value: T | null): void;
(): T;
(value: T | null): void;
peek(): T;
valueHasMutated?:{(): void;};
valueWillMutate?:{(): void;};
peek(): T;
valueHasMutated?:{(): void;};
valueWillMutate?:{(): void;};
extend(requestedExtenders: { [key: string]: any; }): KnockoutObservable<T>;
}
interface KnockoutComputedDefine<T> {
read(): T;
write? (value: T): void;
disposeWhenNodeIsRemoved?: Node;
disposeWhen? (): boolean;
owner?: any;
deferEvaluation?: boolean;
pure?: boolean;
read(): T;
write? (value: T): void;
disposeWhenNodeIsRemoved?: Node;
disposeWhen? (): boolean;
owner?: any;
deferEvaluation?: boolean;
pure?: boolean;
}
interface KnockoutBindingContext {
@@ -189,10 +186,10 @@ interface KnockoutBindingHandlers {
uniqueName: KnockoutBindingHandler;
// Rendering templates
template: KnockoutBindingHandler;
template: KnockoutBindingHandler;
// Components (new for v3.2)
component: KnockoutBindingHandler;
// Components (new for v3.2)
component: KnockoutBindingHandler;
}
interface KnockoutMemoization {
@@ -217,12 +214,12 @@ interface KnockoutVirtualElements {
interface KnockoutExtenders {
throttle(target: any, timeout: number): KnockoutComputed<any>;
notify(target: any, notifyWhen: string): any;
notify(target: any, notifyWhen: string): any;
rateLimit(target: any, timeout: number): any;
rateLimit(target: any, options: { timeout: number; method?: string; }): any;
rateLimit(target: any, timeout: number): any;
rateLimit(target: any, options: { timeout: number; method?: string; }): any;
trackArrayChanges(target: any): any;
trackArrayChanges(target: any): any;
}
//
@@ -352,20 +349,20 @@ interface KnockoutTemplateSourcesDomElement {
}
interface KnockoutTemplateAnonymous extends KnockoutTemplateSourcesDomElement {
nodes(): any;
nodes(value: any): void;
nodes(): any;
nodes(value: any): void;
}
interface KnockoutTemplateSources {
domElement: {
prototype: KnockoutTemplateSourcesDomElement
new (element: Element): KnockoutTemplateSourcesDomElement
prototype: KnockoutTemplateSourcesDomElement
new (element: Element): KnockoutTemplateSourcesDomElement
};
anonymousTemplate: {
prototype: KnockoutTemplateAnonymous;
new (element: Element): KnockoutTemplateAnonymous;
prototype: KnockoutTemplateAnonymous;
new (element: Element): KnockoutTemplateAnonymous;
};
}
@@ -412,26 +409,26 @@ interface KnockoutStatic {
utils: KnockoutUtils;
memoization: KnockoutMemoization;
bindingHandlers: KnockoutBindingHandlers;
getBindingHandler(handler: string): KnockoutBindingHandler;
bindingHandlers: KnockoutBindingHandlers;
getBindingHandler(handler: string): KnockoutBindingHandler;
virtualElements: KnockoutVirtualElements;
extenders: KnockoutExtenders;
applyBindings(viewModelOrBindingContext?: any, rootNode?: any): void;
applyBindingsToDescendants(viewModelOrBindingContext: any, rootNode: any): void;
applyBindingAccessorsToNode(node: Node, bindings: (bindingContext: KnockoutBindingContext, node: Node) => {}, bindingContext: KnockoutBindingContext): void;
applyBindingAccessorsToNode(node: Node, bindings: {}, bindingContext: KnockoutBindingContext): void;
applyBindingAccessorsToNode(node: Node, bindings: (bindingContext: KnockoutBindingContext, node: Node) => {}, viewModel: any): void;
applyBindingAccessorsToNode(node: Node, bindings: {}, viewModel: any): void;
applyBindingsToDescendants(viewModelOrBindingContext: any, rootNode: any): void;
applyBindingAccessorsToNode(node: Node, bindings: (bindingContext: KnockoutBindingContext, node: Node) => {}, bindingContext: KnockoutBindingContext): void;
applyBindingAccessorsToNode(node: Node, bindings: {}, bindingContext: KnockoutBindingContext): void;
applyBindingAccessorsToNode(node: Node, bindings: (bindingContext: KnockoutBindingContext, node: Node) => {}, viewModel: any): void;
applyBindingAccessorsToNode(node: Node, bindings: {}, viewModel: any): void;
applyBindingsToNode(node: Node, bindings: any, viewModelOrBindingContext?: any): any;
subscribable: KnockoutSubscribableStatic;
observable: KnockoutObservableStatic;
computed: KnockoutComputedStatic;
pureComputed<T>(evaluatorFunction: () => T, context?: any): KnockoutComputed<T>;
pureComputed<T>(options: KnockoutComputedDefine<T>, context?: any): KnockoutComputed<T>;
computed: KnockoutComputedStatic;
pureComputed<T>(evaluatorFunction: () => T, context?: any): KnockoutComputed<T>;
pureComputed<T>(options: KnockoutComputedDefine<T>, context?: any): KnockoutComputed<T>;
observableArray: KnockoutObservableArrayStatic;
@@ -447,9 +444,9 @@ interface KnockoutStatic {
cleanNode(node: Element): Element;
renderTemplate(template: Function, viewModel: any, options?: any, target?: any, renderMode?: any): any;
renderTemplate(template: string, viewModel: any, options?: any, target?: any, renderMode?: any): any;
unwrap<T>(value: KnockoutObservable<T> | T): T;
unwrap<T>(value: KnockoutObservable<T> | T): T;
computedContext: KnockoutComputedContext;
computedContext: KnockoutComputedContext;
//////////////////////////////////
// templateSources.js
@@ -557,10 +554,10 @@ interface KnockoutStatic {
/////////////////////////////////
bindingProvider: {
instance: KnockoutBindingProvider;
new (): KnockoutBindingProvider;
}
bindingProvider: {
instance: KnockoutBindingProvider;
new (): KnockoutBindingProvider;
}
/////////////////////////////////
// selectExtensions.js
@@ -599,15 +596,15 @@ interface KnockoutStatic {
}
interface KnockoutBindingProvider {
nodeHasBindings(node: Node): boolean;
getBindings(node: Node, bindingContext: KnockoutBindingContext): {};
getBindingAccessors?(node: Node, bindingContext: KnockoutBindingContext): { [key: string]: string; };
nodeHasBindings(node: Node): boolean;
getBindings(node: Node, bindingContext: KnockoutBindingContext): {};
getBindingAccessors?(node: Node, bindingContext: KnockoutBindingContext): { [key: string]: string; };
}
interface KnockoutComputedContext {
getDependenciesCount(): number;
isInitial: () => boolean;
isSleeping: boolean;
getDependenciesCount(): number;
isInitial: () => boolean;
isSleeping: boolean;
}
//
@@ -688,5 +685,5 @@ interface KnockoutComponents {
declare var ko: KnockoutStatic;
declare module "knockout" {
export = ko;
export = ko;
}

View File

@@ -1,4 +1,4 @@
/// <reference types="knockout.postbox" />
/// <reference types="knockout.mapping" />
declare var $;
@@ -723,4 +723,20 @@ function observableArrayEventsTests() {
count--;
});
}, null, "arrayChange");
}
interface MySubscribable extends KnockoutSubscribable<any> {
isBeautiful?: boolean;
}
interface MyObservable extends KnockoutObservable<any> {
isBeautiful?: boolean;
}
interface MyObservableArray extends KnockoutObservableArray<any> {
isBeautiful?: boolean;
}
interface MyComputed extends KnockoutComputed<any> {
isBeautiful?: boolean;
}