Skip to content

feat: custom component attributes via svelteHTML.ComponentAttributes#3060

Open
khromov wants to merge 2 commits into
sveltejs:masterfrom
khromov:custom-attribute-type
Open

feat: custom component attributes via svelteHTML.ComponentAttributes#3060
khromov wants to merge 2 commits into
sveltejs:masterfrom
khromov:custom-attribute-type

Conversation

@khromov

@khromov khromov commented Jun 16, 2026

Copy link
Copy Markdown

Frameworks built on top of Svelte sometimes attach custom namespaced attributes to components (e.g. <MyComponent custom:defer />). Until now these triggered type errors because the attribute isn't part of the component's props.

You can now declare such attributes globally — and have their values type-checked — by augmenting the svelteHTML.ComponentAttributes interface in a d.ts file (e.g. your app.d.ts):

declare namespace svelteHTML {
    interface ComponentAttributes {
        'custom:defer'?: boolean;
        'custom:hydrate:visible'?: { rootMargin?: string };
    }
}

With this in place:

<MyComponent custom:defer custom:hydrate:visible={{ rootMargin: '200px' }} someOtherProp="foo" />
  • Only attribute names containing a : are treated as custom component attributes.
  • Declared namespaced attributes are validated against their declared types.
  • Regular props keep being checked against the component's own prop definitions.
  • Any namespaced attribute you don't declare is reported as an error, so typos are caught.

How it works

svelte2tsx routes colon-namespaced attributes on components through a new __sveltets_2_componentAttr helper whose parameter is the augmentable svelteHTML.ComponentAttributes interface (added to both shim sets).

Includes docs, a changeset, and htmlx2jsx + language-server diagnostics fixtures.

khromov added 2 commits June 16, 2026 03:20
….ComponentAttributes

Allow declaring extra namespaced attributes (e.g. <MyComponent mochi:defer />)
on any Svelte component through a d.ts augmentation, with the values type-checked
against the declared types and undeclared namespaced attributes reported as errors.

- svelte2tsx: route colon-namespaced attributes on components through a new
  __sveltets_2_componentAttr helper whose parameter is the augmentable
  svelteHTML.ComponentAttributes interface (added to both shim sets).
- Tests: htmlx2jsx fixture + language-server diagnostics fixture.
- Docs + changeset.
Rename the example namespace from mochi: to a generic custom: across docs,
the changeset, shim doc-comments, and the svelte2tsx/language-server fixtures
(inputs and regenerated snapshots).
@khromov khromov changed the title Custom attribute type feat: custom component attributes via svelteHTML.ComponentAttributes Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant