Skip to content

Commit

Permalink
Add And type
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 29, 2024
1 parent 4a40404 commit 9d628aa
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 31 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Click the type names for complete docs.
- [`SharedUnionFieldsDeep`](source/shared-union-fields-deep.d.ts) - Create a type with shared fields from a union of object types, deeply traversing nested structures.
- [`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.

### Type Guard

Expand Down
23 changes: 23 additions & 0 deletions source/and.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type {IsEqual} from './is-equal';

/**
Returns a boolean for whether two given types are both true.
Use-case: Constructing complex conditional types where multiple conditions must be satisfied.
@example
```
import type {And} from 'type-fest';
And<true, true>;
//=> true
And<true, false>;
//=> false
```
*/
export type And<A extends boolean, B extends boolean> = [A, B][number] extends true
? true
: true extends [IsEqual<A, false>, IsEqual<B, false>][number]
? false
: never;
3 changes: 2 additions & 1 deletion source/array-slice.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import type {LessThanOrEqual} from './less-than-or-equal';
import type {GreaterThanOrEqual} from './greater-than-or-equal';
import type {GreaterThan} from './greater-than';
import type {IsNegative} from './numeric';
import type {And, Not, ArrayMin} from './internal';
import type {Not, ArrayMin} from './internal';
import type {IsEqual} from './is-equal';
import type {And} from './and';
import type {ArraySplice} from './array-splice';

/**
Expand Down
3 changes: 2 additions & 1 deletion source/greater-than.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {NumberAbsolute, And, Or, PositiveNumericStringGt} from './internal';
import type {NumberAbsolute, Or, PositiveNumericStringGt} from './internal';
import type {IsEqual} from './is-equal';
import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
import type {And} from './and';

/**
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 whether A and B are both true.
@example
```
And<true, true>;
//=> true
And<true, false>;
//=> false
```
*/
export type And<A extends boolean, B extends boolean> = [A, B][number] extends true
? true
: true extends [IsEqual<A, false>, IsEqual<B, false>][number]
? false
: never;

/**
Returns a boolean for either A or B is true.
Expand Down
9 changes: 1 addition & 8 deletions source/is-never.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ Useful in type utilities, such as checking if something does not occur.
@example
```
import type {IsNever} from 'type-fest';
type And<A, B> =
A extends true
? B extends true
? true
: false
: false;
import type {IsNever, And} from 'type-fest';
// https://github.com/andnp/SimplyTyped/blob/master/src/types/strings.ts
type AreStringsEqual<A extends string, B extends string> =
Expand Down
3 changes: 2 additions & 1 deletion source/subtract.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {NumberAbsolute, BuildTuple, And, Or} from './internal';
import type {NumberAbsolute, BuildTuple, Or} 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';

/**
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,7 +1,8 @@
import type {NumberAbsolute, BuildTuple, And, Or, ArrayMax, ArrayMin} from './internal';
import type {NumberAbsolute, BuildTuple, Or, 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';

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

declare const never: never;

Expand Down

0 comments on commit 9d628aa

Please sign in to comment.