79558601

Date: 2025-04-06 18:59:31
Score: 1
Natty:
Report link

Found a solution based on Typescript convert type Tuple of tuple to Tuple (flatten Tuple):

// Solution using recursive type
type FlattenSortOptions<T> = T extends readonly [infer First, ...infer Rest]
  ? First extends { children: readonly unknown[] }
    ? [...FlattenSortOptions<First["children"]>, ...FlattenSortOptions<Rest>]
    : [First, ...FlattenSortOptions<Rest>]
  : [];

function flattenSortOptions<T extends SortOptions<unknown>>(
  options: T,
): FlattenSortOptions<T> {
  return options.flatMap((option) => {
    if (typeof option === "object" && option !== null && "children" in option) {
      return option.children;
    }
    return option;
  }) as FlattenSortOptions<T>;
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: aeharding