Augmenting external JSX libraries
typed-htmx is extremely minimal and requires the user to manually augment external JSX libraries that provide their own types.
Common guidance
- Create a
types.d.ts
(any name is fine, as long as it ends in.d.ts
) at the top of your src/ folder, or anywhere within the configuredinclude
of your tsconfig.json - Write a JSX element, e.g.
<div />
, and inspect its type - If you see React-related types, you are good to go
- If not, try to discover the common namespace under which all HTML attributes go.
Let's use Hono as an example.
tsx
// In tsconfig.json, jsxImportSource = "hono/jsx"// The type we are augmenting in this case is `Hono.HTMLAttributes`.// hx-boost is not recognized as a proper attribute yet.<div hx-boost ="bogus" />
tsx
// In tsconfig.json, jsxImportSource = "hono/jsx"// The type we are augmenting in this case is `Hono.HTMLAttributes`.// hx-boost is not recognized as a proper attribute yet.<div hx-boost ="bogus" />
With this knowledge, we can now augment the type of Hono.HTMLAttributes
assuming it is an interface:
tsx
/// <reference types="typed-htmx" />declareglobal {namespaceHono {interfaceHTMLAttributes extendsHtmxAttributes {}}}<Type '{ "hx-boost": "bogus"; style: {}; }' is not assignable to type 'HTMLAttributes'. Types of property '["hx-boost"]' are incompatible. Type '"bogus"' is not assignable to type 'BoolStr | undefined'.2322Type '{ "hx-boost": "bogus"; style: {}; }' is not assignable to type 'HTMLAttributes'. Types of property '["hx-boost"]' are incompatible. Type '"bogus"' is not assignable to type 'BoolStr | undefined'.div hx-boost ="bogus"style ={{}}/>
tsx
/// <reference types="typed-htmx" />declareglobal {namespaceHono {interfaceHTMLAttributes extendsHtmxAttributes {}}}<Type '{ "hx-boost": "bogus"; style: {}; }' is not assignable to type 'HTMLAttributes'. Types of property '["hx-boost"]' are incompatible. Type '"bogus"' is not assignable to type 'BoolStr | undefined'.2322Type '{ "hx-boost": "bogus"; style: {}; }' is not assignable to type 'HTMLAttributes'. Types of property '["hx-boost"]' are incompatible. Type '"bogus"' is not assignable to type 'BoolStr | undefined'.div hx-boost ="bogus"style ={{}}/>
Hono
ts
import 'typed-htmx';declareglobal {namespaceHono {interfaceHTMLAttributes extendsHtmxAttributes {}}}
ts
import 'typed-htmx';declareglobal {namespaceHono {interfaceHTMLAttributes extendsHtmxAttributes {}}}
Astro
ts
import 'typed-htmx';declareglobal {namespaceastroHTML .JSX {interfaceIntrinsicAttributes extendsHtmxAttributes {}}}
ts
import 'typed-htmx';declareglobal {namespaceastroHTML .JSX {interfaceIntrinsicAttributes extendsHtmxAttributes {}}}