Sha256: eccffdb59d6d42e3e773756e8bbe1fa8c23f261ef0cef052f3a8c0194dc6a0e0

Contents?: true

Size: 1.83 KB

Versions: 28

Compression:

Stored size: 1.83 KB

Contents

import type {NumberAbsolute, BuildTuple} from './internal';
import type {IsEqual} from './is-equal';
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
import type {LessThan} from './less-than';
import type {Sum} from './sum';
import type {And} from './and';
import type {Or} from './or';

/**
Returns the difference between two numbers.

Note:
- A or B can only support `-999` ~ `999`.
- If the result is negative, you can only get `number`.

@example
```
import type {Subtract} from 'type-fest';

Subtract<333, 222>;
//=> 111

Subtract<111, -222>;
//=> 333

Subtract<-111, 222>;
//=> number

Subtract<PositiveInfinity, 9999>;
//=> PositiveInfinity

Subtract<PositiveInfinity, PositiveInfinity>;
//=> number
```

@category Numeric
*/
// TODO: Support big integer and negative number.
export type Subtract<A extends number, B extends number> = number extends A | B
	? number
	: [
		IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
		IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
	] extends infer R extends [boolean, boolean, boolean, boolean]
		? Or<
		And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
		And<IsEqual<R[3], true>, IsEqual<R[1], false>>
		> extends true
			? PositiveInfinity
			: Or<
			And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
			And<IsEqual<R[2], true>, IsEqual<R[0], false>>
			> extends true
				? NegativeInfinity
				: true extends R[number]
					? number
					: [IsNegative<A>, IsNegative<B>] extends infer R
						? [false, false] extends R
							? BuildTuple<A> extends infer R
								? R extends [...BuildTuple<B>, ...infer R]
									? R['length']
									: number
								: never
							: LessThan<A, B> extends true
								? number
								: [false, true] extends R
									? Sum<A, NumberAbsolute<B>>
									: Subtract<NumberAbsolute<B>, NumberAbsolute<A>>
						: never
		: never;

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts
clapton-0.0.7 lib/clapton/javascripts/node_modules/type-fest/source/subtract.d.ts