Add And type

This commit is contained in:
Sindre Sorhus 2024-04-29 14:24:00 +07:00
parent 4a4040459b
commit 9d628aa623
9 changed files with 34 additions and 31 deletions

View File

@ -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

23
source/and.d.ts vendored Normal file
View File

@ -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;

View File

@ -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';
/**

View File

@ -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.

18
source/internal.d.ts vendored
View File

@ -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.

View File

@ -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> =

View File

@ -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.

3
source/sum.d.ts vendored
View File

@ -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.

View File

@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import type {And} from '../../source/internal';
import type {And} from '../source/and';
declare const never: never;