Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compute to handle arrays #170

Closed
millsp opened this issue Jan 21, 2021 · 3 comments
Closed

Improve compute to handle arrays #170

millsp opened this issue Jan 21, 2021 · 3 comments
Labels
bug Something isn't working
Projects

Comments

@millsp
Copy link
Owner

millsp commented Jan 21, 2021

馃悶 Bug Report

Describe the bug

@millsp I have a question regarding the A.Compute<> type provided. It seems to be incorrectly computing arrays. (e.g. it's pulling them apart as if it's an object instead of keeping it as an array.)

Reproduce the bug

Expected behavior

Possible Solution

import { A, M } from 'ts-toolbelt';
import { Depth } from 'Object/_Internal';
type ComputeFlat<A extends any> = A extends M.BuiltInObject
  ? A
  : A extends Array<any>
  ? A extends Array<Record<string | number | symbol, any>>
    ? Array<
        {
          [K in keyof A[number]]: A[number][K];
        } & {}
      >
    : A
  : A extends ReadonlyArray<any>
  ? A extends ReadonlyArray<Record<string | number | symbol, any>>
    ? ReadonlyArray<
        {
          [K in keyof A[number]]: A[number][K];
        } & {}
      >
    : A
  : {
      [K in keyof A]: A[K];
    } & {};

type ComputeDeep<A extends any> = A extends M.BuiltInObject
  ? A
  : A extends Array<any>
  ? A extends Array<Record<string | number | symbol, any>>
    ? Array<
        {
          [K in keyof A[number]]: ComputeDeep<A[number][K]>;
        } & {}
      >
    : A
  : A extends ReadonlyArray<any>
  ? A extends ReadonlyArray<Record<string | number | symbol, any>>
    ? ReadonlyArray<
        {
          [K in keyof A[number]]: ComputeDeep<A[number][K]>;
        } & {}
      >
    : A
  : {
      [K in keyof A]: ComputeDeep<A[K]>;
    } & {};

export type Compute<A extends any, depth extends Depth = 'deep'> = {
  flat: ComputeFlat<A>;
  deep: ComputeDeep<A>;
}[depth];

type test = {
  a: ReadonlyArray<{
    a: number;
    c: ReadonlyArray<{
      a: number;
    }>;
  }>;
  b: { a: number };
} & {
  a: ReadonlyArray<{
    d: Array<{
      b: number;
    }>;
    c: ReadonlyArray<{
      b: number;
    }>;
  }>;
  c: { c: number };
};

Screenshots

Additional context

@wesselvdv

@millsp millsp added the bug Something isn't working label Jan 21, 2021
@millsp
Copy link
Owner Author

millsp commented Jan 21, 2021

I found that the performance is quite OK. So I'll be adding these changes in the next release of ts-toolbelt. Thanks for your contribution @wesselvdv

@millsp millsp added this to To do in Board via automation Jan 29, 2021
@millsp millsp moved this from To do to Done in Board Jan 30, 2021
@wesselvdv
Copy link

This can be closed I believe @millsp ?

@millsp
Copy link
Owner Author

millsp commented Feb 1, 2021

In a few minutes, yes :)

@millsp millsp closed this as completed in e39ad90 Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Board
  
Done
Development

No branches or pull requests

2 participants