Replies: 2 comments 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.
-
|
@th0rgall yes, svelte 5 is unavoidably more verbose here. this is a known trade-off of the runes design and it's working as intended. why it works this way: in svelte 4, each when you write a few things that were considered and rejected:
the cleanest pattern for your case: interface Props {
accept: string | string[];
disabled?: boolean;
maxSize?: number;
minSize?: number;
multiple?: boolean;
}
let {
accept,
disabled = false,
maxSize = Infinity,
minSize = 0,
multiple = true,
}: Props = $props();for components with many optional props that share patterns, you can use intersection types to reduce repetition: type Validatable = { maxSize?: number; minSize?: number };
type Props = Validatable & {
accept: string | string[];
disabled?: boolean;
multiple?: boolean;
};ref: svelte $props docs | discussion #10690 (brunnerh's explanation) | TS #29526 (partial destructuring types) |
Beta Was this translation helpful? Give feedback.
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