Skip to content

Commit 628afa6

Browse files
committed
fix: register typing
1 parent c6a0452 commit 628afa6

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

deno.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import { documentReady, logEvent } from "./util.ts";
33

44
interface Initializer {
5-
(el: HTMLElement): void;
5+
// deno-lint-ignore no-explicit-any
6+
(el: any): void;
67
/** The elector for the component */
78
sel: string;
89
}
@@ -44,8 +45,8 @@ export interface Context<EL = HTMLElement> {
4445
}
4546

4647
/** The component type */
47-
export type Component = <T extends HTMLElement>(
48-
ctx: Context<T>,
48+
export type Component<EL extends HTMLElement> = (
49+
ctx: Context<EL>,
4950
) => string | undefined | void;
5051

5152
/** The event handler type */
@@ -85,7 +86,10 @@ type MountHook = (el: HTMLElement) => void;
8586
* @param component The component function
8687
* @param name The component name
8788
*/
88-
export function register(component: Component, name: string) {
89+
export function register<EL extends HTMLElement>(
90+
component: Component<EL>,
91+
name: string,
92+
) {
8993
assert(
9094
typeof name === "string" && !!name,
9195
"Component name must be a non-empty string",
@@ -98,7 +102,7 @@ export function register(component: Component, name: string) {
98102
const initClass = `${name}-💊`;
99103

100104
/** Initializes the html element by the given configuration. */
101-
const initializer = (el: HTMLElement) => {
105+
const initializer = (el: EL) => {
102106
if (!el.classList.contains(initClass)) {
103107
// FIXME(kt3k): the below can be written as .add(name, initClass)
104108
// when deno_dom fixes add class.

test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Deno.test("wrong type selector throws with on(selector).event", () => {
305305
register(Component, randomName());
306306
});
307307

308-
Deno.test("register throws with non string input", () => {
308+
Deno.test("register() throws with non string input", () => {
309309
function Component() {}
310310
assertThrows(() => {
311311
// deno-lint-ignore no-explicit-any
@@ -337,7 +337,7 @@ Deno.test("register throws with non string input", () => {
337337
});
338338
});
339339

340-
Deno.test("register throws with already registered name", () => {
340+
Deno.test("register() throws with already registered name", () => {
341341
const name = randomName();
342342
function Component() {}
343343
register(Component, name);
@@ -346,7 +346,7 @@ Deno.test("register throws with already registered name", () => {
346346
});
347347
});
348348

349-
Deno.test("unmount with non registered name throws", () => {
349+
Deno.test("unmount() with non registered name throws", () => {
350350
assertThrows(() => {
351351
unmount(randomName(), document.body);
352352
});
@@ -375,3 +375,11 @@ const queryByClass = (name: string) => {
375375
assertExists(el);
376376
return el;
377377
};
378+
379+
Deno.test("Component with narrower HTML element type works", () => {
380+
function Component({ el }: Context<HTMLDivElement>) {
381+
el.classList.add("foo");
382+
}
383+
384+
register(Component, randomName());
385+
});

0 commit comments

Comments
 (0)