htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Declare global and window types

Open romanzy313 opened this issue 2 years ago • 2 comments

I am having a hard time declaring htmx types globally and on the window object. I load htmx via a script tag in the browser, but I would still like to have type-safety inside my typescript web-components.

Here are my attempts in globals.d.ts:

import * as Htmx from "htmx.org/dist/htmx.d.ts";
import Htmx from "htmx.org/dist/htmx.d.ts";
import Htmx from "htmx.org";

declare global {
  type htmx = Htmx;

  interface Window {
    htmx: Htmx;
  }
}

I think this has to do with the way .d.ts files are generated. Something to do with namespaces... But I have no idea how to make it work

romanzy313 avatar Oct 23 '23 21:10 romanzy313

Try this:

import * as Htmx from 'htmx.org'

declare global {
	interface Window {
		htmx: typeof Htmx
	}
}

i7N3 avatar Jan 29 '24 18:01 i7N3

Thanks to @i7N3, I also got the global htmx working:

import * as Htmx from 'htmx.org';

declare global {
    var htmx: typeof Htmx;

    interface Window {
        htmx: typeof Htmx;
    }
}

MikeMoolenaar avatar Jan 30 '24 19:01 MikeMoolenaar