Similar to as javascript array.
But you would need to specify the types of the parameters and also a return type. Yes you could configure your setup to not care about these things, but then that would defeat the object of using typescript.
export function shuffle<T>(array: T[]): T[] {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
};
You can get different options of shuffle from this question.
Videos
npm
npmjs.com › package › @types › shuffle-array
@types/shuffle-array - npm
declare namespace shuffleArray { /** * copy - Sets if should return a shuffled copy of the given array. By default it's a falsy value. * rng - Specifies a custom random number generator. */ interface ShuffleOptions { copy?: boolean; rng?: () => number; } /** * picks - Specifies how many random elements you want to pick.
» npm install @types/shuffle-array
Published Nov 21, 2023
Version 1.0.5
Quora
quora.com › How-do-you-shuffle-an-array-of-items-using-JavaScript-or-TypeScript
How to shuffle an array of items using JavaScript or TypeScript - Quora
How do transpilers like TypeScript make working with JavaScript better, and why do developers prefer them? ... If memory is not a problem, use two arrays, one filled with random numbers, sort it. With each swap, swap the elements in the array you wanted randomized. When the sorting is done, the other array is shuffled.
Sandro Maglione
sandromaglione.com › articles › software › pure function to shuffle an array in typescript using fp-ts
Pure function to shuffle an array in Typescript using fp-ts | Sandro Maglione
January 16, 2022 - import * as IO from 'fp-ts/IO'; /** Shuffle randomly the given array, algorithm from https://stackoverflow.com/a/12646864/7033357 */ export const shuffle = <T>(array: T[]): IO.IO<T[]> => () => { const source = [...array]; for (let i = source.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [source[i], source[j]] = [source[j], source[i]]; } return source; };
GitBook
basarat.gitbook.io › algorithms › shuffling
Shuffling | Algorithms in TypeScript
December 31, 2019 - Remember random number generation / assiging an item to an array is O(1), so its just n iteration of O(1) ... function shuffleInPlace<T>(array: T[]): T[] { // if it's 1 or 0 items, just return if (array.length <= 1) return array; // For each index in array for (let i = 0; i < array.length; i++) { // choose a random not-yet-placed item to place there // must be an item AFTER the current item, because the stuff // before has all already been placed const randomChoiceIndex = getRandom(i, array.length - 1); // place our random choice in the spot by swapping [array[i], array[randomChoiceIndex]] = [array[randomChoiceIndex], array[i]]; } return array; }
Bomberbot
bomberbot.com › typescript › how-to-shuffle-an-array-of-items-using-javascript-or-typescript
How to Shuffle an Array of Items Using JavaScript or TypeScript - Bomberbot
May 14, 2025 - The space complexity is O(n) in the worst case due to the recursion stack and the temporary arrays created during merging. While this recursive implementation is interesting from a theoretical perspective, the iterative Fisher-Yates shuffle is generally preferable in practice due to its simplicity and optimal O(n) time and O(1) space complexity. One advantage of using TypeScript is that we can define generic functions that work with arrays of any type.
Webdevtutor
webdevtutor.net › blog › typescript-array-shuffle
Mastering Array Shuffling in TypeScript
In conclusion, array shuffling in TypeScript can be achieved using various methods such as the Fisher-Yates shuffle algorithm or utilizing TypeScript libraries like lodash. Understanding these techniques will help you efficiently shuffle arrays in your TypeScript projects.
npm
npmjs.com › package › array-shuffle
array-shuffle - npm
February 2, 2026 - import {arrayToShuffled, arrayShuffle} from 'array-shuffle'; // Create a new shuffled array const shuffled = arrayToShuffled([1, 2, 3, 4, 5, 6]); //=> [3, 5, 4, 1, 2, 6] // Shuffle in-place const array = [1, 2, 3, 4, 5, 6]; arrayShuffle(array); ...
» npm install array-shuffle
Published Feb 02, 2026
Version 4.1.0
Author Sindre Sorhus
GitHub
github.com › neo › shuffle.ts
GitHub - neo/shuffle.ts: Shuffle an Array in TypeScript
November 23, 2017 - Shuffle an Array in TypeScript. Contribute to neo/shuffle.ts development by creating an account on GitHub.
Author neo
egghead.io
egghead.io › lessons › typescript-shuffle-an-array
Shuffle an array | egghead.io
July 27, 2017 - In each iteration i, we simply put a random item from the unshuffled position into the ith position within the shuffled position. Hence, increasing the length of the shuffled position. This way we eventually end up with a fully shuffled array. [02:45] Let's implement this algorithm here in TypeScript.
Webdevtutor
webdevtutor.net › blog › typescript-scramble-array
Scrambling an Array in TypeScript: A Guide to Shuffling and Rearranging Your Data
Another method for scrambling an array is to use the Math.random() function to randomly select elements from the array and swap them. Here's an example implementation of random shuffling in TypeScript: