Skip to content

Commit

Permalink
Add Or type
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 29, 2024
1 parent 9d628aa commit 909c38e
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ Click the type names for complete docs.
- [`DistributedOmit`](source/distributed-omit.d.ts) - Omits keys from a type, distributing the operation over a union.
- [`DistributedPick`](source/distributed-pick.d.ts) - Picks keys from a type, distributing the operation over a union.
- [`And`](source/and.d.ts) - Returns a boolean for whether two given types are both true.
- [`Or`](source/or.d.ts) - Returns a boolean for whether either of two given types are true.

### Type Guard

Expand Down
2 changes: 2 additions & 0 deletions source/and.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ And<true, true>;
And<true, false>;
//=> false
```
@see {@link Or}
*/
export type And<A extends boolean, B extends boolean> = [A, B][number] extends true
? true
Expand Down
3 changes: 2 additions & 1 deletion source/greater-than.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {NumberAbsolute, Or, PositiveNumericStringGt} from './internal';
import type {NumberAbsolute, PositiveNumericStringGt} from './internal';
import type {IsEqual} from './is-equal';
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
import type {And} from './and';
import type {Or} from './or';

/**
Returns a boolean for whether a given number is greater than another number.
Expand Down
18 changes: 0 additions & 18 deletions source/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,24 +451,6 @@ IsPrimitive<Object>
*/
export type IsPrimitive<T> = [T] extends [Primitive] ? true : false;

/**
Returns a boolean for either A or B is true.
@example
```
Or<true, false>;
//=> true
Or<false, false>;
//=> false
```
*/
export type Or<A extends boolean, B extends boolean> = [A, B][number] extends false
? false
: true extends [IsEqual<A, true>, IsEqual<B, true>][number]
? true
: never;

/**
Returns a boolean for whether A is false.
Expand Down
25 changes: 25 additions & 0 deletions source/or.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {IsEqual} from './is-equal';

/**
Returns a boolean for whether either of two given types are true.
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
@example
```
import type {Or} from 'type-fest';
Or<true, false>;
//=> true
Or<false, false>;
//=> false
```
@see {@link And}
*/
export type Or<A extends boolean, B extends boolean> = [A, B][number] extends false
? false
: true extends [IsEqual<A, true>, IsEqual<B, true>][number]
? true
: never;
3 changes: 2 additions & 1 deletion source/subtract.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type {NumberAbsolute, BuildTuple, Or} from './internal';
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.
Expand Down
3 changes: 2 additions & 1 deletion source/sum.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {NumberAbsolute, BuildTuple, Or, ArrayMax, ArrayMin} from './internal';
import type {NumberAbsolute, BuildTuple, ArrayMax, ArrayMin} from './internal';
import type {IsEqual} from './is-equal';
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
import type {Subtract} from './subtract';
import type {And} from './and';
import type {Or} from './or';

/**
Returns the sum of two numbers.
Expand Down
2 changes: 1 addition & 1 deletion test-d/internal/or.ts → test-d/or.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import type {Or} from '../../source/internal';
import type {Or} from '../source/or';

declare const never: never;

Expand Down

0 comments on commit 909c38e

Please sign in to comment.