type-fest
Version:
A collection of essential TypeScript types
70 lines (55 loc) • 3.78 kB
TypeScript
import type {GreaterThan} from './greater-than.d.ts';
import type {NegativeInfinity} from './numeric.d.ts';
import type {UnwrapBrandedPrimitive} from './unwrap-branded-primitive.d.ts';
import type {IsAny} from './is-any.d.ts';
import type {TupleMin} from './tuple-min.d.ts'; // For `@link`
import type {UnionMax} from './union-max.d.ts'; // For `@link`
/**
Returns the maximum number in a tuple of numbers.
Use this to infer the largest configured limit or the return type of a function that wraps `Math.max()`.
Supports readonly tuples, negative numbers, decimals, and infinities. Brands are removed from the result, like {@link UnionMax} does. Numeric enum members are preserved; equal values keep the first element's type. Brands follow the limitations of {@link UnwrapBrandedPrimitive}. Comparisons between numbers printed in exponential notation are unsupported when {@link GreaterThan} cannot decide the result.
Distributes over tuple unions and unions within elements, returning all possible maxima.
Returns {@link NegativeInfinity} for an empty tuple, like `Math.max()`. Returns `number` for arrays with unknown length (including unbounded rest elements) and for a tuple containing a non-literal `number`, like {@link UnionMax}. Returns `any` for `any` or a tuple containing `any`, and `never` for `never`. A `never` element gives `never`, unless a non-literal `number` element makes the result `number`. Tuple union members that give `never` do not add to the result. Optional elements and non-number elements, including `bigint` and `unknown`, are not accepted.
@example
```
import type {TupleMax} from 'type-fest';
const concurrencyLimits = [2, 4, 8] as const;
type MaximumConcurrency = TupleMax<typeof concurrencyLimits>;
//=> 8
type MaximumOffset = TupleMax<[-10, -2, -5]>;
//=> -2
type MaximumScale = TupleMax<[1.25, 1.5, 1.05]>;
//=> 1.5
type PossibleMaximum = TupleMax<[1 | 4, 3]>;
//=> 3 | 4
```
The implementation compares each element with the current maximum, without counting up to the values.
@see {@link TupleMin}
@category Array
@category Numeric
*/
export type TupleMax<Tuple extends readonly number[]> = IsAny<Tuple> extends true
? any
: Tuple extends unknown
? number extends Tuple['length']
? number
: `${number}` extends `${UnwrapBrandedPrimitive<Tuple[number]>}`
// `any` is printed as `${string}`, so it reaches this branch too.
? IsAny<Tuple[number]> extends true ? any : number
: Tuple extends readonly [infer First extends number, ...infer Rest extends number[]]
// The comparisons accept branded values, so the brands are removed only from the result.
? UnwrapBrandedPrimitive<TupleMaxImplementation<Rest, First>>
// The empty tuple returns the identity element of the maximum, not `never`. `Math.max()` returns `-Infinity` at runtime, so `never` would mark reachable code as unreachable. `never` would also disappear from a union, so `TupleMax<[] | [2, 5]>` would be `5` and hide the empty case, and it would look like a `never` input. The identity keeps `TupleMax<[...A, ...B]>` equal in value to `TupleMax<[TupleMax<A>, TupleMax<B>]>` when `A` or `B` is empty, in the same way that `AndAll<[]>` is `true`.
: NegativeInfinity
: never;
type TupleMaxImplementation<Tuple extends readonly number[], Result extends number> = Tuple extends readonly [infer First extends number, ...infer Rest extends number[]]
// Compare each candidate and accumulated value separately so a possible win does not disappear into `boolean`.
? First extends unknown
? Result extends unknown
? GreaterThan<First, Result> extends true
? TupleMaxImplementation<Rest, First>
: TupleMaxImplementation<Rest, Result>
: never // Should never happen
: never // Should never happen
: Result;
export {};