will be an intersection of two types: So our type Flatten will look something like this: 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: Note that we explicitly need to include Array before we exclude all objects, because technically Arrays are also objects. suic86 0 0 Flatten Array. The simplest form of a multi-dimensional array is a two-dimensional array. TypeScript provides quite a lot of ways for you to declare an array… TypeScript infers the type of options to be (string | number)   — an array of arrays containing either strings or numbers. Spoiler alert: the other half is not going to be as easy. This ends up being such a common operation that conditional types make it easier. We can write some useful helper type aliases using the infer keyword. For example, we could have inferred the element type in Flatten instead of fetching it out âmanuallyâ with an indexed access type: Here, we used the infer keyword declaratively introduced a new generic type variable named U instead of specifying how to retrieve the element type of T.
typescript flatten array 2021