Skip to content

fix(svelte): infer props for generic Svelte components#16050

Merged
ematipico merged 2 commits into
withastro:mainfrom
seroperson:i14669-svelte-generic-props
Mar 26, 2026
Merged

fix(svelte): infer props for generic Svelte components#16050
ematipico merged 2 commits into
withastro:mainfrom
seroperson:i14669-svelte-generic-props

Conversation

@seroperson
Copy link
Copy Markdown
Contributor

@seroperson seroperson commented Mar 23, 2026

Closes #14669

Changes

When a Svelte component uses generic type parameters (<script generics="T extends ...">), the editor integration now preserves the generics in the generated TypeScript, so props are correctly inferred and intellisense works in .astro files.

Testing

Added several astro check tests in svelte/test/check.test.js

  • Single-param generic with conditional type constraint
  • Multi-param generic with nested angle brackets (Record<string, unknown>)
  • Arrow function type in generic constraint ((val: string) => boolean)

To check this manually:

  1. Create a Svelte component with generics attribute:
<script lang="ts" generics="T extends boolean = false">
  interface Props { flag?: T; items: T extends true ? number[] : string[] }
  let { flag, items }: Props = $props();
</script>
  1. Import and use it in an .astro file
  2. Before the fix: TypeScript errors and no intellisense
  3. After the fix: props are correctly inferred with generic constraints

Docs

No docs changes are required.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 23, 2026

🦋 Changeset detected

Latest commit: de2dab0

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

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

@github-actions github-actions Bot added pkg: svelte Related to Svelte (scope) pkg: integration Related to any renderer integration (scope) labels Mar 23, 2026
@seroperson seroperson force-pushed the i14669-svelte-generic-props branch 3 times, most recently from aa56f33 to d24f656 Compare March 24, 2026 21:31
@seroperson seroperson force-pushed the i14669-svelte-generic-props branch from d24f656 to 99ed00a Compare March 26, 2026 05:13
Comment thread packages/integrations/svelte/src/editor.cts Outdated
@seroperson seroperson force-pushed the i14669-svelte-generic-props branch from a97172e to 376c4e6 Compare March 26, 2026 12:44
@seroperson seroperson force-pushed the i14669-svelte-generic-props branch from 376c4e6 to de2dab0 Compare March 26, 2026 12:51
@seroperson
Copy link
Copy Markdown
Contributor Author

seroperson commented Mar 26, 2026

VSCode e2e test just for the history

image

@ematipico ematipico merged commit 89a7250 into withastro:main Mar 26, 2026
21 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: integration Related to any renderer integration (scope) pkg: svelte Related to Svelte (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 BUG: Props are not inferred with a Svelte generic component

3 participants