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

Svelte typescript: Props with default values cause Type error of missing props #276

Closed
Dulanjala007 opened this issue Jul 4, 2020 · 10 comments · Fixed by #284
Closed

Svelte typescript: Props with default values cause Type error of missing props #276

Dulanjala007 opened this issue Jul 4, 2020 · 10 comments · Fixed by #284
Labels
feature request New feature or request

Comments

@Dulanjala007
Copy link

Dulanjala007 commented Jul 4, 2020

//component code

<script lang="typescript">
  export let name= '';
</script>

when component is added without providing the Prop "name", vs code shows this error
component = Service

Type '{ service: Service; }' is not assignable to type 'IntrinsicAttributes & { service: Service; className: string; }'.
  Property 'name' is missing in type '{ service: Service; }' but required in type '{ service: Service; name: string; }'

Currently I'm getting rid of this by adding explicit types

<script lang="typescript">
  export let name: string | undefined = '';
</script>

we shouldn't have to do this right, can't we infer the type from the default value assignment?
or are there any other syntax (like '?') to make this more cleaner....
or am I doing something wrong?

@jasonlyu123
Copy link
Member

jasonlyu123 commented Jul 4, 2020

A proposal of this has been discussed here #214 (comment) Might be a good way to improve how the props is determined to be optional

@dummdidumm
Copy link
Member

We did originally not do this because it seemed very hard to track down if the initializer itself is initialized.

Example:

let a: string;
export let b = a;

@jasonlyu123 Or is this no longer relevant / did you find a way to deal with this?

@jasonlyu123
Copy link
Member

Oh! That's an issue. No. I don't have any solution for this.

@dummdidumm dummdidumm added the feature request New feature or request label Jul 4, 2020
@dummdidumm
Copy link
Member

Maybe we could at least relax and fix it for 90% of the cases like this: If initializer is not a variable, mark it as optional.

@dummdidumm
Copy link
Member

Thought about this some more: I think we should mark props as optional as soon as any kind of initializer is given. Typescript itself will make sure that usages are correct, so this is safe to do.

@PatrickG
Copy link
Contributor

PatrickG commented Jul 5, 2020

Thought about this some more: I think we should mark props as optional as soon as any kind of initializer is given. Typescript itself will make sure that usages are correct, so this is safe to do.

Sounds reasonable.
I could rebase my branch and create a PR with my changes.

@dummdidumm
Copy link
Member

Sounds good! One thing about your changes: you removed the undefined check. I think this is still needed because else it is marked as required even if the user types export let bla: string | undefined .

@PatrickG
Copy link
Contributor

PatrickG commented Jul 5, 2020

That's true, like I explained in #214 (comment), this is the correct behavior in my opinion.
Also, if a variable has no initializer and you do not pass the prop to the component, Svelte will log a warning.

But if you think we should still mark a variable as optional if it has the undefined type, I can revert this.

@dummdidumm
Copy link
Member

Mhm, that's true, it's more in line with how the svelte compiler treats it. Also it's a difference in strict mode if you type a prop as undefined or optional I think. You convinced me 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants