Skip to content

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

@Dulanjala007

Description

@Dulanjala007

//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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions