Try this:

const a = [["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"]]
const result = a.reduce((accumulator, value) => accumulator.concat(value), []);
console.log(result)

Answer from Ghoul Ahmed on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › flat
Array.prototype.flat() - JavaScript - MDN Web Docs
However, its elements must be arrays if they are to be flattened. ... const arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] const arr2 = [1, 2, [3, 4, [5, 6]]]; arr2.flat(); // [1, 2, 3, 4, [5, 6]] const arr3 = [1, 2, [3, 4, [5, 6]]]; arr3.flat(2); // [1, 2, 3, 4, 5, 6] const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]; arr4.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ... const arr5 = [1, 2, , 4, 5]; console.log(arr5.flat()); // [1, 2, 4, 5] const array = [1, , 3, ["a", , "c"]]; console.log(array.flat()); // [ 1, 3, "a", "c" ] const array2 = [1, , 3, undefined, ["a", , ["d", , "e"]], null]; console.log(array2.flat()); // [ 1, 3, undefined, "a", ["d", empty, "e"], null ] console.log(array2.flat(2)); // [ 1, 3, undefined, "a", "d", "e", null ]
Discussions

How should I correctly type an array flatten implementation?

There is no perfect way to type this currently, predominantly because recursive types are not allowed. If we could use recursive types, then the solution would be simple:

type Flattened<T> = T extends Array<infer U> ? Flattened<U> : T;

export const flatten = <T>(arr: T[]): Flattened<T>[] => (
  arr.reduce(
    (acc, val) => (
      Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val)
    ),
    [],
  )
);

In general, when a typing would be easily achieved via recursive types, overloads are the (somewhat nasty) solution. Although they obviously will not be able to type the entire domain of the function, we can write overloads for a reasonable sub-domain. For example, in this case we may want to type an array argument of depth up to 3. This can be done with generics:

function flatten<T>(arr: T[][][]): T[]
function flatten<T>(arr: T[][]): T[]
function flatten<T>(arr: T[]): T[]
function flatten<T>(arr: T[]): T[] {
    return arr.reduce(
        (acc, val) => (
            Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val)
        ),
        [],
    );
}
  
const depth1 = flatten([1, 2]); // number[]
const depth2 = flatten([[1], [2]]); // number[]
const depth3 = flatten([[[1]], [[2]]]); // number[]

A slightly subtle point is that the overloaded function definitions are matched from top-to-bottom, so we need to have the deepest definition first. All of the uses of the function would match the final (bottom) overload, however, T would end up being an array-type itself. Hopefully it's clear how this approach could be extended to allow deeper array forms to be used.

This is a relatively frequently used approach - see for example the type definitions for Ramda's compose

Edit: The above approach assumes that all the elements of the array argument have the same depth. If this is not true then you can adjust the types slightly to account for this, e.g.

function flatten<T>(arr: (T[][] | T[] | T)[]): T[]
function flatten<T>(arr: (T[] | T)[]): T[]
function flatten<T>(arr: T[]): T[]
function flatten<T>(arr: T[]): T[] {
    return arr.reduce(
        (acc, val) => (
            Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val)
        ),
        [],
    );
}

const depthVariable = flatten([[[1]], [2], 3]); // number[]
More on reddit.com
🌐 r/typescript
19
4
February 12, 2019
Merge/flatten an array of arrays - javascript
Here's the jsperf link: http://jsperf.com/2-dimensional-array-merge ... I don’t know what’s wrong with the JSPerf because the link is dead, but this is backwards. Repeated concat is not only slower, it’s asymptotically slower. 2022-03-03T03:01:14.93Z+00:00 ... I'm aware that this is hacky, but the must succinct way I know of to flatten ... More on stackoverflow.com
🌐 stackoverflow.com
Flatten an array type to string literals
I don't think you can do quite this, because if you did for example: const input = Array.from(document.querySelectorAll('div')). map(x => x.textContent); myfunc(input, "hello"); There is no way the typescript compiler could guess what the elements of input might be. However, for completely static arrays, you can pass it as the type parameter: function myFunc(item: Flatten) { // Snipped } myFunc<["hello", "world"]>("foo") // bad myFunc<["hello", "world"]>("hello") // ok You could even do: type ValidInput = ['foo', 'bar']; const input2 = ['foo', 'bar'] as ValidInput myFunc('bar'); // ok myFunc('hello'); // bad myFunc('foo'); // ok myFunc('hello'); // bad But in that case, you might as well just do: type ValidStrings = 'hello' | 'world'; myFunc('foo'); TSPlayground link for better highlighting Edit: nice link syntax reddit/tsplayground More on reddit.com
🌐 r/typescript
6
3
August 8, 2018
How to loop through an array of arrays and flatten them
Teachable moment. Try this and tell me what you think... const arrays = [ [1,2,[3,4]], ['a','b',['c']] ] arrays.forEach(array => { array = 'weird'; }) console.log(arrays) More on reddit.com
🌐 r/learnjavascript
9
1
September 8, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › how-to-flatten-array-of-arrays-in-typescript
How to Flatten Array of Arrays in TypeScript ? - GeeksforGeeks
May 2, 2024 - To flatten an array of arrays in TypeScript using forEach, you can iterate over each element of the outer array and then use another forEach loop to handle the inner arrays.
🌐
Gitbooks
schneidenbach.gitbooks.io › typescript-cookbook › content › functional-programming › flattening-array-of-arrays.html
Flattening array of arrays · TypeScript Cookbook
You can combine the power of a new Array, the concat function, and the spread operator to create a new array with all of the objects contained within it: const flattenedArray = [].concat(...nestedArrays);
🌐
Stack Abuse
stackabuse.com › bytes › flattening-array-of-arrays-in-typescript
Flattening Array of Arrays in TypeScript
August 18, 2023 - Array flattening is the process of converting an array of arrays into a single array. In other words, it's about removing the nested structure of a multi-dimensional array. This might be useful, for example, when you need to process all of the elements in a nested array in a sequential manner.
🌐
Luis Web
webdevluis.com › blog › flatten-type-typescript-tutorial-beginners
Building the Flatten Type with Typescript: A Tutorial for Beginners
Learn how to build the Flatten type in typescript, a utility type that takes an array of arrays and flattens it into a single array. This tutorial is perfect for beginners looking to learn more about advanced types in typescript.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › typescript-array-flat-method
TypeScript Array flat() Method - GeeksforGeeks
July 19, 2024 - The Array.prototype.flat() method in TypeScript allows you to create a new array with all sub-array elements concatenated into it, up to a specified depth. This method helps in flattening nested arrays to the desired depth, making it easier ...
🌐
Medium
flut1.medium.com › deep-flatten-typescript-types-with-finite-recursion-cb79233d93ca
Deep-flatten TypeScript types with finite recursion | by Floris Bernard | Medium
January 15, 2023 - Our type Flatten<T> will be an intersection of two types: ... To find all the keys corresponding to non-object values, we’re going to use an approach similar to the mapped type from my previous article: type NonObjectKeysOf<T> = { [K in keyof T]: T[K] extends Array<any> ?
🌐
LogRocket
blog.logrocket.com › home › understanding flatmap() and other typescript arrays
Understanding flatMap() and other TypeScript arrays - LogRocket Blog
June 4, 2024 - In TypeScript, the flat() method is available on arrays and can be used to create a new, one-dimensional flattened array. flat() can take an optional argument that specifies the depth of flattening.
🌐
Reddit
reddit.com › r/typescript › how should i correctly type an array flatten implementation?
How should I correctly type an array flatten implementation? : r/typescript
February 12, 2019 - Sure, as a union type - but that means the return type will also be a union type including all those arrays, even though it represents a value that only includes atomic elements. ... I often use recursive function, but type inference in those is tricky, so I just assert return type with correct mapped type. Here's it's very base case but you can conditionally traverse & transform basically any tree. type NonArray<T> = T extends any[] ? never : T export const flatten = <T>(arr: T[]): Array<NonArray<T>> => { const init: T[] = [] const result = arr.reduce((acc, val) => { if (Array.isArray(val)) { return [...acc, ...flatten(val)] } else { return [...acc, val] } }, init) return result as Array<NonArray<T>> } // (number | (number | number[])[])[] const fixture = [1, 2, [2, 3, [2, 34], 23]] // number[] const test = flatten(fixture)
🌐
GitHub
gist.github.com › carloslfu › 25f7f3cb1ec328fa575a2918c1e725ba
Flatten an array in TypeScript · GitHub
June 2, 2020 - Flatten an array in TypeScript · Raw · README.md · Working code: https://codesandbox.io/s/ancient-snowflake-b8keo · Raw · flatten.test.ts · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Estuary
docs.estuary.dev › how to flatten an array using typescript
How to Flatten an Array Using TypeScript | Estuary Documentation
This guide will show you how to flatten an array field in a collection by creating a TypeScript derivation in Estuary. ... We'll be using TypeScript for our derivation in this guide. Check out our other guides if you're interested in using SQL or Python for transformations. The collection we'll be working with (user_content) contains a field called tags, which is an array of ...
🌐
CoreUI
coreui.io › answers › how-to-flatten-a-nested-array-in-javascript
How to flatten a nested array in JavaScript · CoreUI
September 20, 2025 - In this example, nested.flat() converts [1, [2, 3], [4, [5, 6]]] to [1, 2, 3, 4, [5, 6]], flattening the first level but leaving the deeper nested array [5, 6] intact.
🌐
Flexiple
flexiple.com › javascript › flatten-array-javascript
JavaScript: Flatten an array using different methods - Flexiple
Here when we use the spread operator on the array arr we declared above, the Spread syntax (...) allows the concatenation operation to be performed on all the elements of the array and stores the result in an empty array thus giving us a flattened array.
🌐
SamanthaMing
samanthaming.com › tidbits › 71-how-to-flatten-array-using-array-flat
Flatten Array using Array.flat() in JavaScript | SamanthaMing.com
@bradkovach: Not syntactic sugar. Flat is for collapsing multi-dimensional arrays. [1,2,3, [4,5,6], 789].flat() becomes [1,2,3,4,5,6,7,8,9] Removing undefined indices is a side effect of the flattening process.
🌐
freeCodeCamp
freecodecamp.org › news › flat-and-flatmap-javascript-array-methods
How to Use the flat() and flatMap() Methods to Flatten Arrays in JavaScript
July 26, 2022 - You use the flat() method for concatenating sub-arrays recursively into a single array. The flat() method takes a depth value as its parameter which is optional depending on the depth of the array you wish to flatten (concatenate).