Rest parameters let you collect many values into one place easily. This helps when you don't know how many inputs you will get.
Rest parameters in Svelte
function example(...rest) { // rest is an array of extra arguments } <script> // $$restProps collects undeclared props into an object </script>
In functions, rest parameters use three dots ... before a name to gather extra arguments into an array.
In Svelte components, $$restProps automatically collects all props that are not explicitly declared with export let.
function sum(...numbers) { return numbers.reduce((a, b) => a + b, 0); }
name prop and collects all other props in $$restProps.<script> export let name; </script> <p>Hello {name}!</p> <p>Extra props: {JSON.stringify($$restProps)}</p>
This Svelte component defines a function greet that takes one fixed argument and then any number of names using rest parameters. It joins the names with 'and' and shows the greeting.
<script> // Function using rest parameters function greet(greeting, ...names) { return `${greeting} ${names.join(' and ')}`; } let message = greet('Hello', 'Alice', 'Bob', 'Charlie'); </script> <h1>{message}</h1>
Rest parameters must be the last parameter in a function.
Rest parameters gather arguments into an array, so you can use array methods on them.
In Svelte, rest props help pass unknown or extra props to child components or elements.
Rest parameters collect many arguments into one array.
They make functions and components flexible with inputs.
In Svelte, rest props help handle extra properties easily.
Practice
...props inside a component's export let statement do?Solution
Step 1: Understand rest parameters in Svelte
Rest parameters collect all extra properties passed to a component that are not explicitly declared.Step 2: Analyze the syntax
This syntax gathers all remaining props into an object namedexport let ...propsprops, allowing flexible access.Final Answer:
It collects all extra passed properties into an object calledprops. -> Option CQuick Check:
Rest parameters = collect extra props [OK]
- Thinking rest parameters limit props instead of collecting them
- Confusing rest parameters with event handlers
- Assuming rest parameters auto-bind to state
Solution
Step 1: Recall correct rest parameter syntax in Svelte
Rest parameters use the spread operator...before the variable name inexport let.Step 2: Check each option's syntax
export let ...restProps;correctly usesexport let ...restProps;. Options A, C, and D misuse assignment or spread placement.Final Answer:
export let ...restProps; -> Option AQuick Check:
Rest parameter syntax =export let ...name;[OK]
- Adding assignment (=) with rest parameters
- Placing ... after variable name
- Using invalid syntax like ...restProps = {}
<script>
export let name;
export let ...restProps;
</script>
<h1>Hello {name}!</h1>
<pre>{JSON.stringify(restProps)}</pre>
And this usage:
<MyComponent name="Anna" age={30} city="NY" />What will be rendered inside the
<pre> tag?Solution
Step 1: Understand restProps collects extra props
The declared propnameis separate;restPropscollects all other props likeageandcity.Step 2: Check what JSON.stringify(restProps) outputs
It outputs an object with keysageandcityand their values.Final Answer:
{"age":30,"city":"NY"} -> Option AQuick Check:
Rest props exclude declared props [OK]
- Including declared props inside restProps
- Expecting all props in restProps
- Confusing restProps with a syntax error
<script> export let ...props; console.log(props); </script>
When you try to compile this, what error will you get?
Solution
Step 1: Check rest parameter rules in Svelte
Rest parameters must be the last in the export list and cannot be declared alone if other exports exist.Step 2: Analyze the code snippet
The snippet uses only one rest parameter export, which is valid. However, Svelte currently does not support rest parameters alone in export let statements; this causes a syntax error.Final Answer:
SyntaxError: Rest parameter must be last in export list -> Option DQuick Check:
Rest parameters have strict placement rules [OK]
- Thinking rest parameters require other exports first
- Expecting syntax error on valid single rest declaration
- Confusing rules for multiple exports with single case
<button> element. Which code correctly uses rest parameters to achieve this?Solution
Step 1: Understand Svelte's handling of extra HTML attributes
Svelte does not currently support rest parameters inexport letto collect arbitrary extra HTML attributes.Step 2: Use a single prop with default empty object
Declaringexport let attrs = {};and spreading{...attrs}on the button is the correct way to accept and apply extra attributes.Final Answer:
export let attrs = {}; with <button {...attrs}> -> Option BQuick Check:
Use a single prop object for extra attributes [OK]
- Assigning default empty object to rest parameter
- Not using rest parameter syntax for extra props
- Passing undefined attrs without rest parameter
Ready to prove it?
Pass the test and this pattern moves unknown
