Skip to content

Instantly share code, notes, and snippets.

@betafcc
Last active October 29, 2024 15:39
Show Gist options
  • Save betafcc/f9084dd9f42ffea59ad6f2ff366b864f to your computer and use it in GitHub Desktop.
Save betafcc/f9084dd9f42ffea59ad6f2ff366b864f to your computer and use it in GitHub Desktop.

Revisions

  1. betafcc revised this gist Jul 25, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Permutation.d.ts
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    /**
    * @example
    * type P = Permutation<1 | 2 | 3>
    * // [2, 3, 1] | [3, 2, 1] | [2, 1, 3] | [1, 2, 3] | [3, 1, 2] | [1, 3, 2]
    * // [1, 2, 3] | [1, 3, 2] | [2, 1, 3] | [2, 3, 1] | [3, 1, 2] | [3, 2, 1]
    */
    export type Permutation<U, T = U> = [U] extends [never]
    ? []
  2. betafcc revised this gist Jul 25, 2022. 1 changed file with 5 additions and 12 deletions.
    17 changes: 5 additions & 12 deletions Permutation.d.ts
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,11 @@
    * type P = Permutation<1 | 2 | 3>
    * // [2, 3, 1] | [3, 2, 1] | [2, 1, 3] | [1, 2, 3] | [3, 1, 2] | [1, 3, 2]
    */
    export type Permutation<U> = Flat<NestedPermutation<U>>
    export type Permutation<U, T = U> = [U] extends [never]
    ? []
    : T extends unknown
    ? [T, ...Permutation<Exclude<U, T>>]
    : never

    /**
    * @example
    @@ -19,14 +23,3 @@ export type UnionCount<U> = Permutation<U>['length']
    */
    export type KeyCount<T> = UnionCount<keyof T>

    type NestedPermutation<U> = Recurse<[U, ...(U extends never ? [] : [U])]>

    type Recurse<T extends unknown[]> = T extends [unknown, ...infer Rest]
    ? [NestedPermutation<Exclude<T[0], Rest[number]>>, ...Rest]
    : []

    type Flat<A> = A extends [[never, infer Head], infer Rest]
    ? [...Flat<Head>, Rest]
    : A extends [infer Head, infer Rest]
    ? [...Flat<Head>, Rest]
    : [A]
  3. betafcc revised this gist Jul 24, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Permutation.d.ts
    Original file line number Diff line number Diff line change
    @@ -14,10 +14,10 @@ export type UnionCount<U> = Permutation<U>['length']

    /**
    * @example
    * type C = KeysCount<{ x: 1, y: 2 }>
    * type C = KeyCount<{ x: 1, y: 2 }>
    * // 2
    */
    export type KeysCount<T> = UnionCount<keyof T>
    export type KeyCount<T> = UnionCount<keyof T>

    type NestedPermutation<U> = Recurse<[U, ...(U extends never ? [] : [U])]>

  4. betafcc revised this gist Jul 24, 2022. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Permutation.d.ts
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,13 @@ export type Permutation<U> = Flat<NestedPermutation<U>>
    */
    export type UnionCount<U> = Permutation<U>['length']

    /**
    * @example
    * type C = KeysCount<{ x: 1, y: 2 }>
    * // 2
    */
    export type KeysCount<T> = UnionCount<keyof T>

    type NestedPermutation<U> = Recurse<[U, ...(U extends never ? [] : [U])]>

    type Recurse<T extends unknown[]> = T extends [unknown, ...infer Rest]
  5. betafcc revised this gist Jul 24, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Permutation.d.ts
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ export type Permutation<U> = Flat<NestedPermutation<U>>

    /**
    * @example
    * type C = Permutation<'a' | 'b' | 'c'>
    * type C = UnionCount<'a' | 'b' | 'c'>
    * // 3
    */
    export type UnionCount<U> = Permutation<U>['length']
  6. betafcc revised this gist Jul 24, 2022. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions Permutation.d.ts
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    /**
    * @example
    * type C = Permutation<'a' | 'b' | 'c'>
    * // 3
    * type P = Permutation<1 | 2 | 3>
    * // [2, 3, 1] | [3, 2, 1] | [2, 1, 3] | [1, 2, 3] | [3, 1, 2] | [1, 3, 2]
    */
    export type UnionCount<U> = Permutation<U>['length']
    export type Permutation<U> = Flat<NestedPermutation<U>>

    /**
    * @example
    * type P = Permutation<1 | 2 | 3>
    * // [2, 3, 1] | [3, 2, 1] | [2, 1, 3] | [1, 2, 3] | [3, 1, 2] | [1, 3, 2]
    * type C = Permutation<'a' | 'b' | 'c'>
    * // 3
    */
    export type Permutation<U> = Flat<NestedPermutation<U>>
    export type UnionCount<U> = Permutation<U>['length']

    type NestedPermutation<U> = Recurse<[U, ...(U extends never ? [] : [U])]>

  7. betafcc revised this gist Jul 24, 2022. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions Permutation.d.ts
    Original file line number Diff line number Diff line change
    @@ -12,16 +12,12 @@ export type UnionCount<U> = Permutation<U>['length']
    */
    export type Permutation<U> = Flat<NestedPermutation<U>>

    /**
    * Expects input to be a pair of [Union, Single], will Exclude Single from Union
    * and Recurse to NestedPermutation
    */
    type NestedPermutation<U> = Recurse<[U, ...(U extends never ? [] : [U])]>

    type Recurse<T extends unknown[]> = T extends [unknown, ...infer Rest]
    ? [NestedPermutation<Exclude<T[0], Rest[number]>>, ...Rest]
    : []

    type NestedPermutation<U> = Recurse<[U, ...(U extends never ? [] : [U])]>

    type Flat<A> = A extends [[never, infer Head], infer Rest]
    ? [...Flat<Head>, Rest]
    : A extends [infer Head, infer Rest]
  8. betafcc created this gist Jul 24, 2022.
    29 changes: 29 additions & 0 deletions Permutation.d.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    /**
    * @example
    * type C = Permutation<'a' | 'b' | 'c'>
    * // 3
    */
    export type UnionCount<U> = Permutation<U>['length']

    /**
    * @example
    * type P = Permutation<1 | 2 | 3>
    * // [2, 3, 1] | [3, 2, 1] | [2, 1, 3] | [1, 2, 3] | [3, 1, 2] | [1, 3, 2]
    */
    export type Permutation<U> = Flat<NestedPermutation<U>>

    /**
    * Expects input to be a pair of [Union, Single], will Exclude Single from Union
    * and Recurse to NestedPermutation
    */
    type Recurse<T extends unknown[]> = T extends [unknown, ...infer Rest]
    ? [NestedPermutation<Exclude<T[0], Rest[number]>>, ...Rest]
    : []

    type NestedPermutation<U> = Recurse<[U, ...(U extends never ? [] : [U])]>

    type Flat<A> = A extends [[never, infer Head], infer Rest]
    ? [...Flat<Head>, Rest]
    : A extends [infer Head, infer Rest]
    ? [...Flat<Head>, Rest]
    : [A]