Skip to content

data- attributes should be allowed on components that wrap HTML elements #1825

@dummdidumm

Description

@dummdidumm

Description

<script lang="ts">
  import { Button } from 'carbon-components-svelte';
</script>

<Button on:click={() => alert('do thing')} data-cy={'forwarded-to-html-button'}>Button</Button>

produces

Error: Type '{ "data-cy": string; }' is not assignable to type 'ButtonProps'.
  Object literal may only specify known properties, and '"data-cy"' does not exist in type 'ButtonProps'. (ts)

The error is legitimate since indeed the types for the carbon Button component do not include data-cy. But the Button component does forward it to the HTML button through ...$$restProps, and data- attributes are valid for html elements.

Proposed solution

Best is probably to add an index signature to the html typings in svelte/elements which looks like this:

interface HTMLAttributes {
  [key: `data-${string}`]: any;
}

... but we can't do that until Svelte 4 because Svelte 3 has a minimum version requirement on a TS version that doesn't have that feature yet.
Maybe it's possible to add that inside svelte2tsx in the meantime.

Alternatives

No response

Additional Information, eg. Screenshots

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedFixed in master branch. Pending production release.feature requestNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions