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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

figure out what's up with the ts-toolbelt Tail type, should i just export and us... #252

Open
github-actions bot opened this issue Jul 24, 2023 · 0 comments

Comments

@github-actions
Copy link

https://api.github.com/DetachHead/ts-helpers/blob/bb1ecf7d44ac379a45cb1f9dbf1ebe2977ba3863/src/types/Array.ts#L280

    | T
    | (Dimension extends 0 ? never : DimensionArray<T, Decrement<Dimension>>)
)[]

/**
 * for some reason the ts toolbelt Tail causes a recursion error in the {@link EveryCombination}
 * types so i made this one instead
 */
// TODO: figure out what's up with the ts-toolbelt Tail type, should i just export and use this one everywhere instead?
type Tail2<T extends unknown[]> = number extends T['length'] ? never : Splice<T, 0, 1>

type InsertIntoEachIndexInArray<
    T extends unknown[],
    ToAdd,
    Count extends number,
> = number extends T['length']
    ? never
    : [
          Splice<T, Count, 0, [ToAdd]>,
          ...(Count extends T['length'] ? [] : InsertIntoEachIndexInArray<T, ToAdd, Add<Count, 1>>),
      ]

type AddToCombinations<T extends unknown[][], ToAdd> = T extends []
    ? []
    : [
          ...InsertIntoEachIndexInArray<T[0], ToAdd, 0>,
          ...(Tail2<T> extends infer Narrowed extends unknown[][]
              ? AddToCombinations<Narrowed, ToAdd>
              : never),
      ]

type _EveryCombination<T extends unknown[], Result extends unknown[][] = [[T[0]]]> = T extends []
    ? []
    : T extends [unknown]
    ? Result
    : AddToCombinations<Result, T[1]> extends infer Narrowed extends unknown[][]
    ? _EveryCombination<Tail2<T>, Narrowed>
    : never

/**
 * creates a union type of every possible combination of values in the `T` array type, excluding
 * duplicates.
 *
 * if you want to allow duplicates, do something like `TupleOf<T[number], T['length']>`
 */
export type EveryCombination<T extends unknown[]> = _EveryCombination<T> extends infer Result
    ? Result[number & keyof Result]
    : never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant