Skip to content
Advertisement

ES6 reduce array without typescript error

i want to remove the first array element, without modified the original (immutable),
something like this:

function getArray(): number[] {
   return [1, 2, 3, 4, 5];
}
function getAnother(): number[] {
   const [first, ...rest] = getArray();
   return rest;
}

the code works, but the typescript check complained:

'first' is assigned a value but never used  @typescript-eslint/no-unused-vars

Is there the elegant/better way to do somilar to getAnother()?

Advertisement

Answer

You can ignore an element with a comma.

 const [, ...rest] = getArray();

Array#slice can also be used in this case.

const rest = getArray().slice(1);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement