From d23b19a774645c79a03025036e410855f4bd3f68 Mon Sep 17 00:00:00 2001 From: DetachHead Date: Thu, 20 Jul 2023 00:59:22 +1000 Subject: [PATCH] add `EveryCombination` type for `SortOrder` --- index.d.ts | 100 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 26 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9c8975a..e93bb03 100644 --- a/index.d.ts +++ b/index.d.ts @@ -10,29 +10,77 @@ export interface PluginConfig { export type PrettierConfig = PluginConfig & Config; -type SortOrder = - | 'options-scripts-markup-styles' - | 'options-scripts-styles-markup' - | 'options-markup-styles-scripts' - | 'options-markup-scripts-styles' - | 'options-styles-markup-scripts' - | 'options-styles-scripts-markup' - | 'scripts-options-markup-styles' - | 'scripts-options-styles-markup' - | 'markup-options-styles-scripts' - | 'markup-options-scripts-styles' - | 'styles-options-markup-scripts' - | 'styles-options-scripts-markup' - | 'scripts-markup-options-styles' - | 'scripts-styles-options-markup' - | 'markup-styles-options-scripts' - | 'markup-scripts-options-styles' - | 'styles-markup-options-scripts' - | 'styles-scripts-options-markup' - | 'scripts-markup-styles-options' - | 'scripts-styles-markup-options' - | 'markup-styles-scripts-options' - | 'markup-scripts-styles-options' - | 'styles-markup-scripts-options' - | 'styles-scripts-markup-options' - | 'none'; +type SliceArrayEnd = T extends [ + ...infer Rest extends unknown[], + ...ArrayWithLength>, +] + ? Rest + : never; +type SliceArrayStart = T extends [ + ...ArrayWithLength, + ...infer Rest extends unknown[], +] + ? Rest + : never; +type Tail = SliceArrayStart; + +type ArrayWithLength< + Length extends number, + Array extends unknown[] = [], +> = Array['length'] extends Length ? Array : ArrayWithLength; +type Add = [ + ...ArrayWithLength, + ...ArrayWithLength, +]['length'] & + number; +type Subtract = ArrayWithLength extends [ + ...ArrayWithLength, + ...infer R, +] + ? R['length'] + : never; + +type InsertIntoArray = [ + ...SliceArrayEnd, + ToAdd, + ...SliceArrayStart, +]; + +type InsertIntoEachIndexInArray = [ + InsertIntoArray, + ...(Count extends T['length'] ? [] : InsertIntoEachIndexInArray>), +]; + +type AddToCombinations = T extends [] + ? [] + : [ + ...InsertIntoEachIndexInArray, + ...(Tail extends infer Narrowed extends unknown[][] + ? AddToCombinations + : never), + ]; + +type Join = T extends [] + ? '' + : T['length'] extends 1 + ? `${T[0]}` + : Tail extends infer Narrowed extends string[] + ? `${T[0]}${JoinWith}${Join}` + : never; + +type _EveryCombination = T extends [ + string, +] + ? Result + : _EveryCombination< + /** @ts-expect-error not sure why this doesn't narrow but it */ + Tail, + AddToCombinations + >; + +type EveryCombination = Join< + _EveryCombination extends infer Result ? Result[number & keyof Result] : never, + '-' +>; + +type SortOrder = EveryCombination<['options', 'scripts', 'markup', 'styles']> | 'none';