import { Head, Gte, Tail } from '..'; // Computes the maximum value of an array: https://lodash.com/docs/4.17.15#max. // type S = Max<[1, 2, 3]>; // 3 // export type Max< // The array to iterate over. T extends Array > = // Check if the array is empty, otherwise run the helper function `Maximum` with the first as // the current highest element and the rest of the array. T extends [] ? never : Maximum, Head>; // This type uses recursive (and not officially supported) type alias, see more: // https://github.com/microsoft/TypeScript/issues/26223#issuecomment-513187373. type Maximum< // The array to iterate over. T extends Array, // An internal variable to track the current maximum value. R extends number, // A convenience variable to track the first element of the array. C extends number = Head > = { // Start by checking if we iterated over the entire array. If that's the case, return the maximum // value we found. finish: R; // Next, check if the next element of the array is greater than the current maximum element (R). // If it is, replace it with the first element of the array. replace: Maximum, C>; // Otherwise, skip it, keep `R` as the maximum element and run the recursion again. next: Maximum, R>; }[T extends [] ? 'finish' : Gte extends true ? 'replace' : 'next'];