Code Monkey home page Code Monkey logo

Comments (4)

MajorLift avatar MajorLift commented on July 24, 2024 1

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 and T 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.

albert-luta avatar albert-luta commented on July 24, 2024 1

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.

MajorLift avatar MajorLift commented on July 24, 2024 1

@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.

okadurin avatar okadurin commented on July 24, 2024

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 with K. Otherwise T after extends is "shadowing" the original T
  • ? [H, T] -> ? [H, ...K]
    ** T could be replaced with K as explained in the previous point
    ** There should be ... before K. Otherwise K is a tuple

from type-challenges-solutions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.