mirror of
https://github.com/sindresorhus/type-fest.git
synced 2026-02-01 15:59:43 +00:00
Add And type
This commit is contained in:
parent
4a4040459b
commit
9d628aa623
@ -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
23
source/and.d.ts
vendored
Normal 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;
|
||||
3
source/array-slice.d.ts
vendored
3
source/array-slice.d.ts
vendored
@ -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';
|
||||
|
||||
/**
|
||||
|
||||
3
source/greater-than.d.ts
vendored
3
source/greater-than.d.ts
vendored
@ -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
18
source/internal.d.ts
vendored
@ -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.
|
||||
|
||||
|
||||
9
source/is-never.d.ts
vendored
9
source/is-never.d.ts
vendored
@ -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> =
|
||||
|
||||
3
source/subtract.d.ts
vendored
3
source/subtract.d.ts
vendored
@ -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
3
source/sum.d.ts
vendored
@ -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.
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import {expectType} from 'tsd';
|
||||
import type {And} from '../../source/internal';
|
||||
import type {And} from '../source/and';
|
||||
|
||||
declare const never: never;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user