Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/data-attribute-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@solidjs/web": patch
"@solidjs/h": patch
---

`data-*` attributes type-check in props objects (`ComponentProps<"div">`, `JSX.HTMLAttributes<T>`), not only in JSX: a template-literal index signature on `ElementAttributes` (#4082).
7 changes: 7 additions & 0 deletions packages/h/jsx-runtime/src/jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,13 @@ export namespace JSX {
extends CustomAttributes<T>, PropAttributes, EventHandlersElement<T>, AriaAttributes {
// [key: ClassKeys]: boolean;

/**
* `data-*` attributes. JSX already accepts hyphenated names; this types
* them in props objects too (`ComponentProps<"div">`, a spread's source).
* `true` writes `""`; `false` and `undefined` remove the attribute.
*/
[key: `data-${string}`]: FunctionMaybe<string | number | boolean | undefined>;

// properties
innerHTML?: FunctionMaybe<string>;
textContent?: FunctionMaybe<string | number>;
Expand Down
7 changes: 7 additions & 0 deletions packages/web/jsx/jsx-h.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,13 @@ export namespace JSX {
extends CustomAttributes<T>, PropAttributes, EventHandlersElement<T>, AriaAttributes {
// [key: ClassKeys]: boolean;

/**
* `data-*` attributes. JSX already accepts hyphenated names; this types
* them in props objects too (`ComponentProps<"div">`, a spread's source).
* `true` writes `""`; `false` and `undefined` remove the attribute.
*/
[key: `data-${string}`]: FunctionMaybe<string | number | boolean | undefined>;

// properties
innerHTML?: FunctionMaybe<string>;
textContent?: FunctionMaybe<string | number>;
Expand Down
7 changes: 7 additions & 0 deletions packages/web/jsx/jsx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,13 @@ export namespace JSX {
extends CustomAttributes<T>, PropAttributes, EventHandlersElement<T>, AriaAttributes {
// [key: ClassKeys]: boolean;

/**
* `data-*` attributes. JSX already accepts hyphenated names; this types
* them in props objects too (`ComponentProps<"div">`, a spread's source).
* `true` writes `""`; `false` and `undefined` remove the attribute.
*/
[key: `data-${string}`]: string | number | boolean | undefined;

// properties
innerHTML?: string;
textContent?: string | number;
Expand Down
20 changes: 20 additions & 0 deletions packages/web/test/data-attributes.type-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// `data-*` attributes in props objects (#4082): JSX already accepts
// hyphenated names on intrinsic elements, and an object typed with the same
// element's props now accepts them too, so a function returning props to
// spread type-checks without a cast.
import type { ComponentProps, JSX } from "@solidjs/web";

export const attrs: JSX.HTMLAttributes<HTMLDivElement> = { "data-state": "open" };
export const props: ComponentProps<"div"> = { role: "button", "data-state": "open" };

function disclosure(): ComponentProps<"button"> {
return { "aria-expanded": "false", "data-state": "closed", "data-count": 2, "data-open": false };
}
export const forwarded: ComponentProps<"button"> = { ...disclosure(), "data-extra": true };

type P = Omit<ComponentProps<"div">, "ref">;
export const withClass: P = { "data-x": "1", class: "a" };
// @ts-expect-error unknown non-data keys are still rejected
export const unknownKey: P = { foo: 1 };
// @ts-expect-error data values stay primitive
export const objectValue: P = { "data-x": {} };
Loading