Skip to content

How to type a prop in svelte 5 (Typescript)? #10690

Answered by brunnerh
rgeditz asked this question in Q&A
Discussion options

You must be logged in to vote

without having to add types to all the other props (since they can be inferred)

They cannot be inferred because this is the left side of an assignment, the only thing that can be inferred is the type of the default values, which do not necessarily have anything to do with the values provided by whatever is on the right side of the assignment.

The resulting type of the variables will be an intersection of both.

I do not know of any way to avoid typing everything, the only minor optimization would be adding a utility type for { children: Snippet }.

So it's either of:

let { ... }: {
  children: Snippet,
  actionUrl?: string,
  submitFunction?: SubmitFunction,
} = $props();

let { ... }: Ha…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@brunnerh
Comment options

@rgeditz
Comment options

Answer selected by rgeditz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants