import type {Negate} from './negate.d.ts'; import type {NonNegativeIntegerSum} from './internal/index.d.ts'; import type {UnwrapBrandedPrimitive} from './unwrap-branded-primitive.d.ts'; import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric.d.ts'; import type {Subtract} from './subtract.d.ts'; import type {Absolute} from './absolute.d.ts'; import type {StringToNumber} from './string-to-number.d.ts'; /** Returns the sum of two numbers. Note: - The result is exact while A and B are within `Number.MAX_SAFE_INTEGER`. A larger number is already rounded, so the result can then be `number` or a literal that differs from what JavaScript computes. - A or B must be written without a decimal point or exponent. Such a value resolves to `never` rather than a silently truncated whole number. A union that mixes both kinds is not caught. @example ``` import type {Sum, PositiveInfinity, NegativeInfinity} from 'type-fest'; type A = Sum<111, 222>; //=> 333 type B = Sum<-111, 222>; //=> 111 type C = Sum<111, -222>; //=> -111 type D = Sum; //=> Infinity type E = Sum; //=> number type F = Sum<999_999, 1>; //=> 1000000 ``` @category Numeric */ export type Sum = SumImplementation : A, B extends object ? UnwrapBrandedPrimitive : B>; // Normalize operands once before validating their digits and computing the result. type SumImplementation = // A decimal fraction or exponent notation is not supported, so the result is `never` rather than a silently truncated whole number. `${A}|${B}` extends `${string}${'.' | 'e'}${string}` ? never // Every pair must reach the infinity checks independently, including opposite infinities. : A extends unknown ? B extends unknown // Read an enum member as its plain literal, so the shortcuts in `_Sum` do not return the member itself. ? _Sum, StringToNumber<`${B}`>> : never : never; type _Sum = // Match the widened number by its printed type, because numeric enum members also accept `number`. `${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 Negate ? 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 ? NonNegativeIntegerSum : AreNegative extends [true, true] // When both numbers are negative we add the absolute values and then reverse the sign ? Negate, Absolute>> // When the signs are different, subtract the negative operand's magnitude from the positive operand. // Subtract already determines the result's sign, so there is no need to compare the magnitudes again. : IsNegative extends true // The second operand is positive, so start with it. ? Subtract> // The first operand is positive, so start with it. : Subtract>; export {};