🌐
Built In
builtin.com › software-engineering-perspectives › array-slice-javascript
3 Ways to Use Array Slice in JavaScript | Built In
Using the shift and pop method, you can remove an element at the front and end of the array respectively. The splice() method can also be used remove elements at a specific index, but it modifies the original array. Using slice() allows you to achieve the same result without changing the source array.
🌐
Mimo
mimo.org › glossary › javascript › array-slice
JavaScript Array slice() Method: Syntax, Usage, and Examples
As part of the built-in Array ... or create new arrays from existing ones. The slice() method returns a shallow copy of a portion of an array into a new array object, selected from a start index to an end index (end not included)....
🌐
Fjolt
fjolt.com › article › javascript-slice
Javascript Array Slice Method
The slice method is used to return shallow copies of arrays based on optional start and end arguments. Shallow copies contain the same reference as the original array sliced.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › splice
Array.prototype.splice() - JavaScript | MDN
To create a new array with a segment removed and/or replaced without mutating the original array, use toSpliced(). To access part of an array without modifying it, see slice().
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
For example, TypeScript knows that only a string value will have a typeof value "string": ... Notice that in the else branch, we don’t need to do anything special - if x wasn’t a string[], then it must have been a string. Sometimes you’ll have a union where all the members have something in common. For example, both arrays and strings have a slice method.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › lets-clear-up-the-confusion-around-the-slice-splice-split-methods-in-javascript-8ba3266c29ae
Let’s clear up the confusion around the slice( ), splice( ), & split( ) methods in JavaScript
October 9, 2018 - This usage is valid in JavaScript. An array with different data types: string, numbers, and a boolean. The slice( ) method copies a given part of an array and returns that copied part as a new array.
🌐
Reddit
reddit.com › r/javascript › what's the difference between doing [].slice.call(array) and array.slice()? i've seen such []s used many times with the array inside the call. why? thanks!
What's the difference between doing [].slice.call(array) and ...
July 3, 2018 -

EDIT: Thanks for the answers, magnificent people! So Array.from is a better way than [].slice.call, and they're used to provide the array prototype to array-like objects! If done directly, apparently errors are thrown.

Thanks!

🌐
W3Schools
w3schools.com › js › tryit.asp
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › slice
Array.prototype.slice() - JavaScript | MDN
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-array-slice-method
TypeScript Array slice() Method - GeeksforGeeks
August 8, 2024 - The Array.slice() method in TypeScript is a built-in function used to extract a section of an array and return it as a new array.
🌐
TutorialsPoint
tutorialspoint.com › home › typescript › typescript array slice
TypeScript Array Slice
December 18, 2016 - TypeScript - null vs. undefined ... begin − Zero-based index at which to begin extraction. As a negative index, start indicates an offset from the end of the sequence. end − Zero-based index at which to end extraction. Returns the extracted array based on the passed parameters. var arr = ["orange", "mango", "banana", "sugar", "tea"]; console.log("arr.slice( 1, 2) : " + arr.slice( 1, 2) ); console.log("arr.slice( 1, 3) : " + arr.slice( 1, 3) );
🌐
W3Schools
w3schools.com › jsref › jsref_slice_array.asp
W3Schools.com
The slice() method returns selected elements in a new array.
🌐
daily.dev
daily.dev › home › blog › get into tech › js array slice: a beginner's guide
JS Array Slice: A Beginner's Guide
December 22, 2025 - If you're diving into JavaScript, understanding how to work with arrays is crucial. The slice() method is a beginner-friendly tool that lets you extract parts of an array without altering the original.
🌐
HowToDoInJava
howtodoinjava.com › home › typescript › typescript array slice()
TypeScript Array slice() with Examples
August 29, 2023 - In TypeScript, the array.slice() method extracts a section of an array (slice of an array) and returns it as a new array.
🌐
Educative
educative.io › answers › what-is-arrayslice-in-typescript
What is array.slice() in TypeScript?
TypeScript is a superset of JavaScript. Wherever JavaScript runs, TypeScript runs, too. We can use the slice() method in TypeScript. This method is used to extract a certain section or parts of the elements of an array.
Top answer
1 of 1
2

“If [ignorance is] the answer than fine.” Nah, you’re not ignorant. If you’re ignorant, then I’m ignorant. You’re exploring the type system and doing interesting things, and you’re using good linter rules. That leads to correct, but sometimes unintuitive errors.

Yes, you can do what you’re asking (code examples at end):

  • Example 1: In the general case where you split arguments of known types, your usage example is essentially correct, except for one case: say you want to operate on the first element and then recursively pass the rest, as you’re doing here - the array could be of length zero, which would result in a TypeScript compiler error. You can handle the above safely using Tuples, coupled with Array destructuring instead of Array.slice(). Typescript accepts destructuring, because it statically (pre-runtime) assigns specific indices of the array to specific variables. TypeScript rejects slice(), because by definition it returns an arbitrary portion of the array. (To understand why, here’s something messed up: JavaScript runtime actually accepts Array.slice(NaN), or Array.slice(0, -Infinity) without throwing an error, so it would be very nonsensical for TypeScript to try to statically infer type from such an absurd function.)
  • Example 2: In your specific case where you split arguments of any type, you can use the unknown type instead. TypeScript treats unknown types, unlike any types, as basically useless until their type is verified. (In your case, your linter throws an error, but without your linter, TypeScript would happily allow let a: any[] = []; let b = [...a]. However, TypeScript does allow an unknown to be passed to a function that accepts a type of any, e.g. console.log(...args: any[]).

Examples

1. General

function Input(
  a: string,
  b: number,
  c: boolean | unknown,
  ...whatever: unknown[],
) {
  // body
}

function ProcessFirst(
  a: string,
) {
  // body
}

function ProcessRest(
  b: number,
  ...whateverOrC: unknown[],
) {
  // body
}

/**
 * Because Input has three named arguments,
 * Parameter<typeof Input> gives a Tuple type:
 *
 * inputs: [
 *   string,
 *   number,
 *   boolean | unknown,
 *   ...unknown[]
 * ]
 */
const Processor = (
  ...inputs: Parameters<typeof Input>,
) => {

  // ProcessFirst: ([string]) => {}
  ProcessFirst(
    inputs[0], // OK: TypeScript knows this is string
  );

  // ProcessRest: ([number, ...unknown[]]) => {}
  // Use array destructuring to get inputs[1] and inputs.slice(2):
  const [ , b, ...whateverOrC] = inputs; // first ',' skips inputs[0]

  ProcessRest(
    b, // OK: TypeScript knows this is number
    ...whateverOrC, // OK: TypeScript knows this is [boolean | unknown, ...unknown[]]
      // -> boolean | unknown extends unknown
      // -> [boolean | unknown, ...unknown[]] extends [unknown, ...unknown[]]
      // -> [unknown, ...unknown[]] extends unknown[]
      // Therefore, whateverOrC extends unknown[]
  );

  return;
}

2. Your Case

const log = (
  ...args: [unknown, ...unknown[]]
) => {
  const [first, ...rest] = args;

  // console[`log`]: (any[]) => {}
  console.log(
    `foo: ${first}`, // OK: TypeScript knows first is unknown extends any
    ...rest, // OK: TypeScript knows rest is unknown[] extends any[]
  );
}

// log: ([unknown, ...unknown[]]) => {}
log(5, null); // OK:    [number, {}]
log(5);       // OK:    [number]
log();        // ERROR: [] 
log(...[]);   // ERROR: []
🌐
Dasha
dasha.ai › blog › javascript-arrays-slice-and-splice
Dasha | Slice and Splice JavaScript Arrays Like a Pro
December 7, 2021 - The array slice javascript method copies a chunk (or slice) from an array and returns that copied part as a new array. It does not modify or change the original array. Instead, it creates a new shallow copy of the original array.