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

🐛 BUG: Svelte optional props: code works but vscode complains #314

Closed
1 task
felixsanz opened this issue Jun 4, 2022 · 5 comments · Fixed by withastro/astro#3993
Closed
1 task
Assignees

Comments

@felixsanz
Copy link

felixsanz commented Jun 4, 2022

What version of astro are you using?

v1.0.0-beta.40

Are you using an SSR adapter? If so, which one?

no

What package manager are you using?

npm

What operating system are you using?

linux

Describe the Bug

Using typescript this complains on vscode, it tells you that the signature doesn't mach and you're missing a prop.

Component1.astro

---
import Component2 from "./Component2.svelte";
---
<Component2 />

Component2.svelte

<script lang="ts">
  export let optional: string = "none";
</script>

<h1>{optional}</h1>

Link to Minimal Reproducible Example

NA

Participation

  • I am willing to submit a pull request for this issue.
@Princesseuh Princesseuh transferred this issue from withastro/astro Jun 5, 2022
@felixsanz felixsanz changed the title 🐛 BUG: Svelte optional props doesn't work 🐛 BUG: Svelte optional props: code works but vscode complains Jun 5, 2022
@Princesseuh Princesseuh added the bug label Jun 5, 2022
@felixsanz
Copy link
Author

felixsanz commented Jun 8, 2022

By the way confirming that the code works 100%, it's just vscode showing an error:

(alias) function Component2(_props: typeof Props): any
import Component2
Type '{}' is not assignable to type 'IntrinsicAttributes & { optional: string }'.
  Type '{}' is missing the following properties from type '{ optional: string }': optional ts(2322)

@Princesseuh
Copy link
Member

Princesseuh commented Jun 8, 2022

It seems like the type that Svelte gives us doesn't properly include that a certain props might be optional (notice no ? after the prop inside the error message)

It might be something on our end but I'm feeling like this could be an upstream incompatibly, I'll have to check how Svelte handle this

@felixsanz
Copy link
Author

I ported this component from sveltekit + svelte and it works fine there

@shaunchander
Copy link

It seems like the type that Svelte gives us doesn't properly include that a certain props might be optional (notice no ? after the prop inside the error message)

It might be something on our end but I'm feeling like this could be an upstream incompatibly, I'll have to check how Svelte handle this

I believe Svelte checks if props have a defined value. If it does then it marks it as optional?

I can also confirm this is an issue on OS X.

Also, I noticed that if you use a union type, for example: export let prop: string | undefined = "", the reported type for prop when using the component is just string. It seems that undefined is just dropped (which could be something on Svelte or Astro's end).

@Princesseuh
Copy link
Member

It seems like the type that Svelte gives us doesn't properly include that a certain props might be optional (notice no ? after the prop inside the error message)

It might be something on our end but I'm feeling like this could be an upstream incompatibly, I'll have to check how Svelte handle this

I believe Svelte checks if props have a defined value. If it does then it marks it as optional?

Yeah, this is how it works in Svelte itself, if a prop has a default value it's considered optional

I investigated a bit and the issue here is that due to how we get props for Svelte components (just taking the props parameter used in the render function), we don't get if a prop is optional or not, just a list of the props with no real information

The way the Svelte tooling handle this is through a function that takes two parameters, the optional props and the required ones and return a list with the proper types / optional markers through some TS trickery.

I think we should be able to implement that fairly easily into our tooling too (famous last words)

Also, I noticed that if you use a union type, for example: export let prop: string | undefined = "", the reported type for prop when using the component is just string. It seems that undefined is just dropped (which could be something on Svelte or Astro's end).

This could be a separate issue on our end but I know that the Svelte tooling had some issues with this in the past too. Maybe this will be fixed by fixing the main issue

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 a pull request may close this issue.

3 participants