Skip to content
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

fix: make $props() rune non-generic #10694

Merged
merged 2 commits into from
Mar 12, 2024
Merged

fix: make $props() rune non-generic #10694

merged 2 commits into from
Mar 12, 2024

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Mar 5, 2024

The $props() rune should not accept a type argument. Instead, component authors should type their props like any other variable declaration:

-let { message } = $props<{ message: string }>();
+let { message }: { message: string } = $props();

Otherwise, things can break because you're relying on inference:

image

In the above image you can see that the type is widened from string to string | number because when using the generics notation, TypeScript takes the initializer into account when generating the type - which is not what we want.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

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

Copy link

changeset-bot bot commented Mar 5, 2024

🦋 Changeset detected

Latest commit: c19754c

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
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

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

Let me quickly test if language-tools also support the non-generic notation

@jgb-solutions
Copy link

Damn I'll need to update my code again. LOL!

@merisbahti
Copy link

merisbahti commented Mar 18, 2024

Is this a reasonable regex (with replace) to do the migration?

I'm using vscodes find/replace function, the search regex is: = \$props<(.*)>\(\) and the replace is: : $1 = $props()

Before

image

#After
image

@dummdidumm
Copy link
Member

Yeah that looks sensible

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.

5 participants