import type {TupleOf} from './tuple-of.d.ts'; import type {NumberAbsolute, TupleMax, ReverseSign} from './internal/index.d.ts'; import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric.d.ts'; import type {Subtract} from './subtract.d.ts'; /** Returns the sum of two numbers. Note: - A or B can only support `-999` ~ `999`. @example ``` import type {Sum} from 'type-fest'; Sum<111, 222>; //=> 333 Sum<-111, 222>; //=> 111 Sum<111, -222>; //=> -111 Sum; //=> PositiveInfinity Sum; //=> number ``` @category Numeric */ // TODO: Support big integer. export type Sum = // Handle cases when A or B is the actual "number" type number extends A | B ? number // Handle cases when A and B are both +/- infinity : A extends B & (PositiveInfinity | NegativeInfinity) ? A // A or B could be used here as they are equal // Handle cases when A and B are opposite infinities : A | B extends PositiveInfinity | NegativeInfinity ? number // Handle cases when A is +/- infinity : A extends PositiveInfinity | NegativeInfinity ? A // Handle cases when B is +/- infinity : B extends PositiveInfinity | NegativeInfinity ? B // Handle cases when A or B is 0 or it's the same number with different signs : A extends 0 ? B : B extends 0 ? A : A extends ReverseSign ? 0 // Handle remaining regular cases : SumPostChecks; /** Adds two numbers A and B, such that they are not equal with different signs and neither of them are 0, +/- infinity or the `number` type */ type SumPostChecks, IsNegative]> = AreNegative extends [false, false] // When both numbers are positive we can add them together ? SumPositives : AreNegative extends [true, true] // When both numbers are negative we add the absolute values and then reverse the sign ? ReverseSign, NumberAbsolute>> // When the signs are different we can subtract the absolute values, remove the sign // and then reverse the sign if the larger absolute value is negative : NumberAbsolute, NumberAbsolute>> extends infer Result extends number ? TupleMax<[NumberAbsolute, NumberAbsolute]> extends infer Max_ extends number ? Max_ extends A | B // The larger absolute value is positive, so the result is positive ? Result // The larger absolute value is negative, so the result is negative : ReverseSign : never : never; /** Adds two positive numbers. */ type SumPositives = [...TupleOf, ...TupleOf]['length'] extends infer Result extends number ? Result : never; export {};