Comments (4)
There are two issues with this solution:
Given the following test case...
// @ts-expect-error
type error = Flatten<undefined>
- The
T extends []
check is redundant. T is an empty array iff. T is an array andT extends [infer H, ...infer R]
is false.
type Flatten<T extends unknown[]> =
T extends [infer H, ...infer Rest]
? H extends unknown[]
? [...Flatten<H>, ...Flatten<Rest>]
: [H, ...Flatten<Rest>]
: []
- The solution uses
T
ambiguously -- for both the input array type and the inferred "tail" array type. Here's a slightly different formulation that relies on those two types being defined independently.
type Flatten<T extends any[]> = // T[0] cannot extend unknown
T extends [infer H, ...infer Rest]
? H extends unknown[]
? [...Flatten<T[0]>, ...Flatten<Rest>]
: [T[0], ...Flatten<Rest>]
: []
from type-challenges-solutions.
My solution is similar with @MajorLift one, except I'm using Flattern
1 instantiation less :P
type Flatten<T extends unknown[]> = T extends [infer Head, ...infer Tail]
? Head extends unknown[]
? Flatten<[...Head, ...Tail]>
: [Head, ...Flatten<Tail>]
: [];
from type-challenges-solutions.
@albert-luta Nice! It seems that your solution would result in less optimal recursion depth, as the Flatten
call will attempt to process the entire input array (with Head
one level flattened) on a single stack. Depending on how deeply nested the elements of T
are, this could result in a significant performance hit.
from type-challenges-solutions.
I believe this initial example could be improved
type Flatten<T> = T extends []
? []
: T extends [infer H, ...infer T]
? [H, T]
: [T];
: T extends [infer H, ...infer T]
->: T extends [infer H, ...infer K]
T
could be replaced withK
. Otherwise T afterextends
is "shadowing" the originalT
? [H, T]
->? [H, ...K]
** T could be replaced with K as explained in the previous point
** There should be...
before K. Otherwise K is atuple
from type-challenges-solutions.
Related Issues (20)
- type-challenges-solutions/en/medium-merge HOT 5
- easy-includes HOT 3
- type-challenges-solutions/en/medium-return-type HOT 4
- type-challenges-solutions/en/medium-zip HOT 3
- type-challenges-solutions/en/medium-trim HOT 3
- type-challenges-solutions/en/medium-length-of-string HOT 1
- type-challenges-solutions/en/medium-objectentries HOT 4
- type-challenges-solutions/en/medium-nomiwase HOT 1
- type-challenges-solutions/en/medium-unique HOT 1
- type-challenges-solutions/en/hard-camelcase HOT 1
- type-challenges-solutions/en/easy-if HOT 7
- type-challenges-solutions/en/easy-parameters HOT 3
- type-challenges-solutions/en/medium-percentage-parser HOT 2
- type-challenges-solutions/en/medium-without HOT 1
- type-challenges-solutions/en/medium-partialbykeys HOT 1
- type-challenges-solutions/en/medium-shift HOT 1
- type-challenges-solutions/en/medium-replace HOT 1
- type-challenges-solutions/zh/easy-awaited HOT 1
- type-challenges-solutions/en/medium-number-range HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from type-challenges-solutions.