Creating custom knockout bindings in typescript.

This content is more than 3 years old. Please read this keeping its age in mind.

In Javascript you create a custom knockout.js binding like this

//Initialise the binding to the value in the input
ko.bindingHandlers.initValue = {
    init: function (element, valueAccessor) {
        valueAccessor()($(element).val());
    }
};

But in TypeScript you’ll get the error

Property 'initValue' does not exist on type 'KnockoutBindingHandlers'.

Just create a definition file for your custom bindings, something like knockout.bindings.d.ts and provide a definition for your binding.

interface KnockoutBindingHandlers {
    initValue: KnockoutBindingHandler;
}

Now it will transpile correctly.

Noticed an error or omission? Please look at submitting a pull request.