Defining extenders in typescript

166 views
Skip to first unread message

Mario Valle

unread,
Mar 24, 2020, 1:07:03 PM3/24/20
to KnockoutJS
I'm using ko with great satisfaction for a few years. Now I have moved to typescript for my projects and ko is supported very well by "@types/knockout": "^3.4.67" types.

My problem is that I cannot correctly add a new extender. I tried to add the example from https://knockoutjs.com/documentation/extenders.html but tsc complained that "Property 'logChange' does not exist on type 'KnockoutExtenders'."

Instead, adding new bindingHandlers works correctly.

Seems that the interface KnockoutExtenders is missing something like [bindingHandler: string]: KnockoutBindingHandler; present in the KnockoutBindingHandlers interface.

Is it a bug or a feature?
Thanks!
mario

jks...@jkshay.com

unread,
Mar 24, 2020, 2:53:26 PM3/24/20
to knock...@googlegroups.com
Do you need to add a custom typings file?

Sent from my iPhone

On Mar 24, 2020, at 1:08 PM, Mario Valle <silicon...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/knockoutjs/f7705b9a-566e-4b31-93c4-51188b9af0bc%40googlegroups.com.

Murat Duisenbayev

unread,
Mar 24, 2020, 8:52:58 PM3/24/20
to knock...@googlegroups.com
I found this on stackoverflow:

ko.extenders seems to be of type KnockoutExtenders, which is like this in the typings file:
interface KnockoutExtenders {
    throttle(target: any, timeout: number): KnockoutComputed<any>;
    notify(target: any, notifyWhen: string): any;
    rateLimit(target: any, timeout: number): any;
    rateLimit(target: any, options: { timeout: number; method?: string; }): any;
    trackArrayChanges(target: any): any;
}

It seems that it contains definitions for basic extenders that KO has by default. And in order to add a new extender's definition there you need to extend the KnockoutExtenders interface:

interface KnockoutExtenders {
    logChange(target: any, option: any): KnockoutObservable<any>;
}
ko.extenders.logChange = function (target: KnockoutObservable<any>, option: any) {
    target.subscribe(function (newValue) {
        console.log(option + ": " + newValue);
    });
    return target;
};

Hope this helps! 

вт, 24 мар. 2020 г. в 18:07, Mario Valle <silicon...@gmail.com>:

Mario Valle

unread,
Mar 26, 2020, 3:43:27 AM3/26/20
to KnockoutJS
Thanks! But I tried, and nothing changed. I don't understand why I cannot convince typescript to merge interface definitions if one is from node_modules @types and the other is local. Does this works for you?
mario
To unsubscribe from this group and stop receiving emails from it, send an email to knock...@googlegroups.com.

Murat Duisenbayev

unread,
Mar 26, 2020, 11:00:28 AM3/26/20
to knock...@googlegroups.com
It seems to be working for me.
What exactly is being the obstacle while trying to use this method? What is the error message you are getting
Reply all
Reply to author
Forward
0 new messages