svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Extend props from HTML Element

Open TheHadiAhmadi opened this issue 2 years ago • 1 comments

we can update ElProps type to also extend HTML Attributes. https://github.com/sveltejs/language-tools/issues/442#issuecomment-1338090276


<script lang="ts">
  interface $$Props extends svelte.JSX.HTMLAttributes<HTMLButtonElement> {
    error: boolean; // error is explicitly typed as boolean to consumers of the component
  }

  export let error; // error is implicitly typed as any within the component
</script>

<button class:error {...$$restProps}>
  <slot />
</button>

TheHadiAhmadi avatar Dec 17 '22 15:12 TheHadiAhmadi

Heads up, you should use the new types coming with Svelte version v3.55.0 instead:

<script lang="ts">
  import type { HTMLButtonAttributes } from 'svelte/elements';
  interface $$Props extends HTMLButtonAttributes  {
    error: boolean; // error is explicitly typed as boolean to consumers of the component
  }

  export let error; // error is implicitly typed as any within the component
</script>

<button class:error {...$$restProps}>
  <slot />
</button>

dummdidumm avatar Dec 20 '22 12:12 dummdidumm

Heads up, you should use the new types coming with Svelte version v3.55.0 instead:

<script lang="ts">
  import type { HTMLButtonAttributes } from 'svelte/elements';
  interface $$Props extends HTMLButtonAttributes  {
    error: boolean; // error is explicitly typed as boolean to consumers of the component
  }

  export let error; // error is implicitly typed as any within the component
</script>

<button class:error {...$$restProps}>
  <slot />
</button>

dummdidumm avatar Dec 20 '22 12:12 dummdidumm