Skip to content

fix: add types for SVG bindable attributes #16525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025
Merged

Conversation

ejb
Copy link
Contributor

@ejb ejb commented Jul 30, 2025

The following props are bindable in SVG elements, but the type definition only includes them for HTML elements.

  • contentRect
  • contentBoxSize
  • borderBoxSize
  • devicePixelContentBoxSize
  • clientWidth
  • clientHeight

This means that currently, svelte-check throws an error for this line:

<text x="0" y="14" bind:contentRect={null, rect => console.log({rect})}>svg element</text>

Here's the error:

Error: Object literal may only specify known properties, and '"bind:contentRect"' does not exist in type 'HTMLProps<"text", SVGAttributes<any>>'. (ts)

I moved the definitions for these attributes from HTMLAttributes to the parent DOMAttributes object so they can be used by both HTML and SVG elements.


Weirdly, direct binding (without get/set functions) like below does not get flagged as an error. I'm not sure why.

<text x="0" y="14" bind:contentRect={rect}>svg element</text>

That might indicate there is a deeper issue here in the typechecking process. But in the meantime this updated type definition seems to solve the issue. :)

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jul 30, 2025

🦋 Changeset detected

Latest commit: de8f20f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@16525

Copy link
Contributor

@7nik 7nik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@7nik 7nik merged commit 72e46d3 into sveltejs:main Jul 31, 2025
11 checks passed
@github-actions github-actions bot mentioned this pull request Jul 31, 2025
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.

2 participants