Skip to content

Commit 937a5a6

Browse files
committed
fix: allow store with narrower type than control value in bind()
Widens the bind overload to accept WritableAtom<SV> where SV extends the control's value type CV, permitting stores typed more narrowly than the element's value property.
1 parent cc9280f commit 937a5a6

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

packages/nanotags/src/setup-context.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { atom } from "nanostores";
1+
import { atom, type WritableAtom } from "nanostores";
22
import { afterEach, describe, expect, expectTypeOf, it, vi } from "vitest";
33

44
import { define } from "./define";
@@ -658,4 +658,21 @@ describe("bind", () => {
658658
const el = {} as HTMLElement;
659659
ctx.bind(atom("x"), el, { prop: "theme" });
660660
});
661+
662+
it.skipIf(true)("accepts store with narrower type than control value", () => {
663+
// oxlint-disable-next-line typescript-eslint/no-empty-object-type
664+
const ctx = {} as SetupContext<{}, {}>;
665+
const $narrow = atom("light") as WritableAtom<"light" | "dark">;
666+
const ctrl = {} as HTMLElement & { value: string };
667+
ctx.bind($narrow, ctrl);
668+
});
669+
670+
it.skipIf(true)("rejects store with wider type than control value", () => {
671+
// oxlint-disable-next-line typescript-eslint/no-empty-object-type
672+
const ctx = {} as SetupContext<{}, {}>;
673+
const $wide = atom("") as WritableAtom<string>;
674+
const ctrl = {} as HTMLElement & { value: "light" | "dark" };
675+
// @ts-expect-error - string does not extend "light" | "dark"
676+
ctx.bind($wide, ctrl);
677+
});
661678
});

packages/nanotags/src/setup-context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ export class Context<
211211
control: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement,
212212
opts?: BindOptions,
213213
): void;
214-
bind<V>(
215-
store: WritableAtom<V>,
216-
control: HTMLElement & { value: NoInfer<V> },
214+
bind<CV, SV extends CV>(
215+
store: WritableAtom<SV>,
216+
control: HTMLElement & { value: CV },
217217
opts?: BindOptions,
218218
): void;
219219
bind(store: WritableAtom<unknown>, control: HTMLElement, opts: BindOptions): void;

0 commit comments

Comments
 (0)