Skip to content

mount() does not enforce generic component prop constraints #17658

@janosh

Description

@janosh

Describe the bug

mount() doesn't enforce generic component prop constraints. The Component<Props> type erases the component-level generic T, so Props resolves to the default/widened type and TypeScript can't enforce the extends constraint on props passed to mount().

Expected: type error when props violate the component's generic constraint.
Actual: invalid props silently accepted.

Note: using the component in a template does correctly catch the error.

Reproduction

GenericList.svelte

<script lang="ts" generics="T extends { id: number; label: string }">
  let { items = [] }: { items: T[] } = $props()
</script>

{#each items as item}
  <div>{item.label} (#{item.id})</div>
{/each}

App.svelte

<script lang="ts">
  import { mount } from 'svelte'
  import GenericList from './GenericList.svelte'

  // should error: { foo: 42 } doesn't satisfy { id: number; label: string }
  // but mount() accepts it without complaint
  mount(GenericList, {
    target: document.body,
    props: { items: [{ foo: 42 }] },
  })
</script>

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions