diff --git a/types/knockout/index.d.ts b/types/knockout/index.d.ts index 5ac6943eb4..1340e22d8f 100644 --- a/types/knockout/index.d.ts +++ b/types/knockout/index.d.ts @@ -17,7 +17,7 @@ interface KnockoutComputedFunctions { } interface KnockoutObservableFunctions { - equalityComparer(a: any, b: any): boolean; + equalityComparer(a: T, b: T): boolean; } interface KnockoutObservableArrayFunctions { @@ -78,6 +78,10 @@ interface KnockoutComputedStatic { interface KnockoutComputed extends KnockoutObservable, KnockoutComputedFunctions { fn: KnockoutComputedFunctions; + // It's possible for a to be undefined, since the equalityComparer is run on the initial + // computation with undefined as the first argument. This is user-relevant for deferred computeds. + equalityComparer(a: T | undefined, b: T): boolean; + dispose(): void; isActive(): boolean; getDependenciesCount(): number; diff --git a/types/knockout/test/index.ts b/types/knockout/test/index.ts index 38f439efb7..130527818e 100644 --- a/types/knockout/test/index.ts +++ b/types/knockout/test/index.ts @@ -601,6 +601,13 @@ function test_misc() { } } + ko.observable("foo").equalityComparer = (a, b) => { + return a.toLowerCase() === b.toLowerCase(); + }; + ko.computed(() => "foo").equalityComparer = (a, b) => { + return (a !== undefined) && a.toLowerCase() === b.toLowerCase(); + }; + } interface KnockoutBindingHandlers {