bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.56k stars 1.31k forks source link

typing for `defineExtension` should allow partials #3030

Closed anentropic closed 7 hours ago

anentropic commented 18 hours ago

Description

Docs for custom extensions show:

https://htmx.org/extensions/building/

To define an extension you call the htmx.defineExtension() function:

<script>
  htmx.defineExtension('my-ext', {
    onEvent : function(name, evt) {
        console.log("Fired event: " + name, evt);
    }
  })
</script>

Typically, this is done in a stand-alone javascript file, rather than in an inline script tag. ... Extensions can override the following default extension points to add or change functionality:

However the current typing doesn't allow this - it requires a fully populated HtmxExtension object defining all the methods:

{
    init: function(api) {return null;},
    getSelectors: function() {return null;},
    onEvent : function(name, evt) {return true;},
    transformResponse : function(text, xhr, elt) {return text;},
    isInlineSwap : function(swapStyle) {return false;},
    handleSwap : function(swapStyle, target, fragment, settleInfo) {return false;},
    encodeParameters : function(xhr, parameters, elt) {return null;}
}

Testing

I made a working extension in my project and ignored the typing errors. (This is what prompted making this PR)

Changing the type to Partial<HtmxExtension> in my installed copy of htmx.esm.d.ts makes the typing errors go away and seems to reflect intended usage.

Checklist