Questions tagged [rest-parameters]

36 questions
83
votes
11 answers

Spread Syntax vs Rest Parameter in ES2015 / ES6

I am confused about the spread syntax and rest parameter in ES2015. Can anybody explain the difference between them with proper examples?
52
votes
6 answers

What is the meaning of "...args" (three dots) in a function definition?

It was really confusing for me to read this syntax in Javascript: router.route('/:id') .put((...args) => controller.update(...args)) .get((...args) => controller.findById(...args)); What does ...args mean?
Cesar Jr Rodriguez
  • 1,691
  • 4
  • 22
  • 35
38
votes
1 answer

Typed function parameters using destructuring and rest in TypeScript

I have a function: export default ({ input: { name, onChange, value, ...restInput }, meta, ...rest }) => ( ... ); Given that 'name' is a string, 'onChange' is a function, 'value' is a string, 'meta' is an object, how can I add types to…
user7605119
19
votes
3 answers

Is It Possible To Set Default Parameter Value On A Rest Parameter

ES6 introduces a bevy of convenient "syntactic sugar". Among them are the default parameter capabilities of JavaScript functions, as well as rest parameters. I'm finding that my console (or devTools) complains (i.e., throws an error) whenever…
IsenrichO
  • 4,251
  • 3
  • 18
  • 31
19
votes
6 answers

Usage of rest parameter and spread operator in javascript

What's the usage of rest parameter that will be added in ECMAScript 6? For example, in ECMAScript 5 you can do the following to get an array of parameters starting from the second element: // ES 5 store('Joe', 'money'); store('Jane', 'letters',…
Tareq Salah
  • 3,720
  • 4
  • 34
  • 48
9
votes
2 answers

Why are the rest parameters in JavaScript called so?

Why are rest parameters in JavaScript called so?
Phi_1.618
  • 191
  • 1
  • 1
  • 7
8
votes
5 answers

javascript es6: use case for destructuring rest parameter

I just saw a code snippet in MDN about destructuring rest parameters like so: function f(...[a, b, c]) { return a + b + c; } f(1) // NaN (b and c are undefined) f(1, 2, 3) // 6 f(1, 2, 3, 4) // 6 (the fourth parameter is not…
user3303864
4
votes
0 answers

Why typescript does not narrow in this case (rest parameter)?

EDIT: Link to a MRE (thanks @Thomas) I have these types : export declare type Matrix2D = [ number, number, number, number, number, number, ]; export declare type Matrix3D = [ number, number, number, number, number, number, number,…
hl037_
  • 3,520
  • 1
  • 27
  • 58
4
votes
1 answer

Property 'className' does not exist on type '{ props: ReactNode; }'

I am currently migrating a Next.js project from JavaScript to TypeScript, and I've run into an error: Property 'className' does not exist on type '{ props: ReactNode; }'. In Javascript, I can extract className from props but typescript cannot find…
Gravy59
  • 67
  • 2
  • 5
4
votes
2 answers

js array remove element by value using destructuring/rest syntax

Seemed quite intuitive to me, but turns out that's not how things work! The goal is to remove the passed element if it exists and return the rest. I know there's a number of ways of achieving this - including filter: const rest = selection.filter(i…
kawerewagaba
  • 1,107
  • 2
  • 15
  • 21
4
votes
1 answer

JS string destructuring: rest parameter returning inconsistent data

Consider the following examples An old project: const [x, ...y] = "text"; console.log(x) // "t" console.log(y) // "ext" A new project based on CRA: const [x, ...y] = "text"; console.log(x) // "t" console.log(y) // ["e", "x", "t"] I'm not sure why…
3
votes
1 answer

Is it possible to define a set of rest arguments in TypeScript?

I don't know if the question title was sugestive but my problem is, in a constructor I got a rest parameter that must be the keys of an object and I want that, when I call the constructor, it only allows the keys that isn't yet used. I don't know if…
2
votes
1 answer

Difference between assignment and unpacking intto one array to another

So I Have this code in JavaScript what is the deffernce between the Line A and Line B const arr1 = [1,2,3,4,5] const arr2 = [...arr1]; // Line A const arr2 = arr1; // Line B So I want to know is it the same or there's some difference between…
2
votes
1 answer

unpacking rest (...theArgs) parameter before passing it to the function

I am trying to call a function required number of times. To solve this, I am creating a function that takes rest parameters and iterating through the first argument. I am unable to unpack the rest arguments and pass it to the function. Is there a…
user2678648
  • 157
  • 1
  • 1
  • 9
2
votes
1 answer

How to specify multiple conditions in an array and call it in an if statement in javascript

I don't know this can be possible or not I want to store all my condition in array and need to call it in if statement const addition = (...numbers) => { let arrayOfTest = [ `${numbers.length === 0}`, `${numbers.some(isNaN)}`, …
Aakash
  • 139
  • 1
  • 3
  • 22
1
2 3