Replies: 1 comment 1 reply
-
|
Hii @th0rgall, In Svelte 5, to mix inferred defaults with explicit types, use interface ExplicitProps { accept: string | string[] }
let {
accept,
disabled = false,
maxSize = Infinity,
minSize = 0,
multiple = true,
preventDropOnDocument = true,
noClick = false,
noKeyboard = false,
noDrag = false,
noDragEventsBubbling = false,
containerClasses = '',
containerStyles = '',
disableDefaultStyles = false,
name = ''
}: Partial<ExplicitProps> & typeof $props() = $props();
✅ Rule of thumb: use explicit types for required props, defaults handle inference for the rest. If this answer solved all your doubts, please mark it as solved. If not, please tell me your doubt. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I'm doing a sizeable Svelte 4 -> 5 migration, and I've encountered some components which have many props with default values.
In Svelte 4, Svelte/TS could infer the types of the props using their default values. For those props that did not have a default value, it was possible to explicitly specify their type while keeping inference for the rest.
I haven't found a way to do this in Svelte 5 without explicitly specifying the type for each property. Am I missing something?
Here's an excerpt from the codebase with a few default values:
Svelte 4:
In Svelte 5, two changes are needed, as far as I could find:
What doesn't work:
Is the conclusion that Svelte 5 is just unavoidably more verbose in this situation?
Beta Was this translation helpful? Give feedback.
All reactions