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
The flat() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length. If the element is not an array, it's directly appended to the result. If the element is an array, it's flattened according to the depth parameter.
Discussions

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
hacking my way through typing this flatten function.
I would do it like this . Please note how I did the recursive typings. P.S. Also it's a really bad practice to use Function type. You would greatly benefit from specifying types of your functions precisely. If you don't know how to write type of, e.g., array's reduce, you can just "extract" it using field accessor syntax: type ReduceFn = Array['reduce']; More on reddit.com
🌐 r/typescript
6
2
June 29, 2019
🌐
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
To make sure you're only getting arrays of Person back, you can cast your array to be of type Person[]to give you an extra layer of type safety: const flattenedArray = ([] as Person[]).concat(...nestedArrays);
🌐
Stack Abuse
stackabuse.com › bytes › flattening-array-of-arrays-in-typescript
Flattening Array of Arrays in TypeScript
August 18, 2023 - Here, reduce() starts with an empty array as the accumulator (the second argument to reduce(), []) and then concatenates each sub-array to it. The result is a new flattened array. Note: The reduce() method does not change the original array. Instead, it returns a new array. This is an important point to remember when working with array methods in TypeScript.
🌐
Estuary
docs.estuary.dev › how to flatten an array using typescript
How to Flatten an Array Using TypeScript | Estuary Documentation
You'll see a basic structure for your TypeScript code. It should look something like this: import { IDerivation, Document, SourceUserContent, } from "flow/sean-estuary/test-derivation.ts"; export class Derivation extends IDerivation { userContent(_read: { doc: SourceUserContent }): Document[] { throw new Error("Not implemented"); } } Now, let's modify the userContent function to flatten the array.
Find elsewhere
🌐
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 › flatten an array type to string literals
r/typescript on Reddit: Flatten an array type to string literals
August 8, 2018 -

Hey everyone, maybe I'm overthinking this but I'm struggling with flattening the type of an array of strings to string literals. What I'm looking to do:

type Flatten<T extends string | string[]> = T extends string ? T : T[number];

function myFunc<T extends string | string[]>(input: T, item: Flatten<T>) {
  // Snipped
}

const input = ["hello", "world"]

// This should fail because "foo" is not in the array. However, it works because the type is just string instead of "hello" | "world"
myFunc(input, "foo")

// And this should work because hello is in the array, but again it will take anything that's a string.
myFunc(input, "hello")

// This does work, it only allows "hello" as the second argument
myFunc("hello", "foo") // Error! "foo" is not assignable to "hello"

When I pass in ["hello", "world"] to myFunc, I'm trying to get the input type to be constrained to "hello" | "world". I can't seem to get that to work. However, it does work if I just pass in "hello", then the input type is constrained to the string "hello".

Am I overlooking something here, or is this not possible?

🌐
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> ?
🌐
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.
🌐
Exercism
exercism.org › tracks › typescript › exercises › flatten-array › solutions
Community solutions for Flatten Array in TypeScript on Exercism
Explore other people's solutions to Flatten Array in TypeScript, and learn how others have solved the exercise.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › flatMap
Array.prototype.flatMap() - JavaScript | MDN
Note that in this particular case the flatMap approach is slower than the for-loop approach — due to the creation of temporary arrays that must be garbage collected, as well as the return array not needing to be frequently resized. However, flatMap may still be the correct solution in cases where its flexibility and readability are desired. ... const arr = [1, 2, 3, 4]; arr.map((x) => [x * 2]); // [[2], [4], [6], [8]] arr.flatMap((x) => [x * 2]); // [2, 4, 6, 8] // only one level is flattened arr.flatMap((x) => [[x * 2]]); // [[2], [4], [6], [8]]
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › 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 ...
🌐
Catchts
catchts.com › flatten
How to flatten a tuple type in TypeScript?
@captain-yossarian TypeScript blog, fully dedicated to static typings and type safety
🌐
Reddit
reddit.com › r/typescript › hacking my way through typing this flatten function.
r/typescript on Reddit: hacking my way through typing this flatten function.
June 29, 2019 -

Thanks to several of you here who helped me begin using recursive types, something i didn't even realize was possible.

I've been playing around with it trying to cement the concept.

The use case here was that this flatten function below wouldn't accept a number array, TS would complain about the nesting, and the recursive type solved this problem.

A flatten function that accepts nested number arrays and returns a flat number array.

However, trying to type this thing turned into lots of trial and error and the result seems hacky to me, and could be implemented much better.

The first snag of inputting the nestedArrayOrNumber<Number> type into the function was that TS complained that reduce did not exist on the type. And the hacking began when i just tried adding reduce : Function, to the interface, and the error went away! I wasn't expecting that to work honestly.

In this simple case the only extra method i had to the interface was reduce, but for all other methods available to Array, i would have to add them to the interface individually, which doesn't feel right to me.

The idea that sprang to mind, but that i was unable to implement, was trying to do a type union with the aim of : when this nestedArrayOrNumber<Number> is a number array, treat it as a Array<Number> so that we have access to all the Array methods, however, when it is a primitive number, treat it as a primitive number.

Currently the biggest confusion for me is : i was able to type "inner" on line 9, as either a number [ ], or a

nestedArrayOrNumber<Number> , and TS accepted both. But my hunch was, and it was proved with that console.log, that inner is in fact a nested number array, which made me confused as to why typing it as a number [ ] worked at all, since the whole point of the recursive type was because TS complained about passing a number [ ] as an argument to the function in the first place... any idea why the input to the function cannot be number [ ], but inside the reduce function, number [ ] will type check?

How would you recommend i improve this implementation?

apologizes for the long post, i figured it be better to be thorough in my thought process. Of course thank you for the time and generosity, i've been learning much from you.

🌐
TestMu AI Community
community.testmuai.com › ask a question
Why are flatMap, flat, and flatten not recognized on any[] in TypeScript? - TestMu AI Community
November 6, 2024 - Why is flatMap, flat, and flatten not recognized on any[] in TypeScript? I’m using Chrome 70, which supports methods like .flatMap, .flatten, and .flat. My code runs as expected in the browser, but TypeScript doesn’t recognize these methods. For example: // data.flatMap lint error export const transformData = (data: any[]) => data.flatMap(abc => [ parentObj(abc), ...generateTasks(abc) ]); The warning I get is TS2339: Property ‘flatMap’ does not exist on type ‘any[]’. I am using Angula...
🌐
egghead.io
egghead.io › lessons › typescript-use-typescript-conditional-types-to-create-a-reusable-flatten-type
Use TypeScript conditional types to create a reusable Flatten type | egghead.io
If I try to pass in a Boolean in here, TypeScript is going to throw an error. This is where conditional types are super handy. They allow me to build a single, all-purpose, and need type called flatten, which will accept anything. [03:11] It's first going to check if the passed in type extends an array...
Published   October 7, 2018
🌐
GitHub
gist.github.com › robinduckett › 6e59aaadf0f74f1f68602a9ca8b0d202
Flatten Typescript Array · GitHub
const array: MultiDimensionalArray<number> = [[1], [[2]], [[3], 4]]; flatten(array) // returns [1, 2, 3, 4]
🌐
Medium
blog.swmansion.com › deep-flatten-typescript-type-c0d123028d82
Deep-flatten TypeScript type | by Błażej Kustra | Jan, 2024 | Software Mansion
January 9, 2024 - Nested object types can be a real pain to work with, especially when recursion comes into play. You can flatten an object with nested…