type-fest
Advanced tools
Comparing version 4.11.1 to 4.12.0
@@ -49,2 +49,3 @@ // Basic | ||
export type {Stringified} from './source/stringified'; | ||
export type {StringSlice} from './source/string-slice'; | ||
export type {FixedLengthArray} from './source/fixed-length-array'; | ||
@@ -76,3 +77,10 @@ export type {MultidimensionalArray} from './source/multidimensional-array'; | ||
NonNegativeInteger, | ||
IsNegative, | ||
} from './source/numeric'; | ||
export type {GreaterThan} from './source/greater-than'; | ||
export type {GreaterThanOrEqual} from './source/greater-than-or-equal'; | ||
export type {LessThan} from './source/less-than'; | ||
export type {LessThanOrEqual} from './source/less-than-or-equal'; | ||
export type {Sum} from './source/sum'; | ||
export type {Subtract} from './source/subtract'; | ||
export type {StringKeyOf} from './source/string-key-of'; | ||
@@ -109,2 +117,3 @@ export type {Exact} from './source/exact'; | ||
export type {ArrayValues} from './source/array-values'; | ||
export type {ArraySlice} from './source/array-slice'; | ||
export type {ArraySplice} from './source/array-splice'; | ||
@@ -111,0 +120,0 @@ export type {SetFieldType} from './source/set-field-type'; |
{ | ||
"name": "type-fest", | ||
"version": "4.11.1", | ||
"version": "4.12.0", | ||
"description": "A collection of essential TypeScript types", | ||
@@ -5,0 +5,0 @@ "license": "(MIT OR CC0-1.0)", |
@@ -242,2 +242,3 @@ <div align="center"> | ||
- [`Replace`](source/replace.d.ts) - Represents a string with some or all matches replaced by a replacement. | ||
- [`StringSlice`](source/string-slice.d.ts) - Returns a string slice of a given range, just like `String#slice()`. | ||
@@ -248,2 +249,3 @@ ### Array | ||
- [`Join`](source/join.d.ts) - Join an array of strings and/or numbers using the given string as a delimiter. | ||
- [`ArraySlice`](source/array-slice.d.ts) - Returns an array slice of a given range, just like `Array#slice()`. | ||
- [`LastArrayElement`](source/last-array-element.d.ts) - Extracts the type of the last element of an array. | ||
@@ -268,2 +270,9 @@ - [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length. | ||
- [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer. | ||
- [`IsNegative`](source/numeric.d.ts) - Returns a boolean for whether the given number is a negative number. | ||
- [`GreaterThan`](source/greater-than.d.ts) - Returns a boolean for whether a given number is greater than another number. | ||
- [`GreaterThanOrEqual`](source/greater-than-or-equal.d.ts) - Returns a boolean for whether a given number is greater than or equal to another number. | ||
- [`LessThan`](source/less-than.d.ts) - Returns a boolean for whether a given number is less than another number. | ||
- [`LessThanOrEqual`](source/less-than-or-equal.d.ts) - Returns a boolean for whether a given number is less than or equal to another number. | ||
- [`Sum`](source/sum.d.ts) - Returns the sum of two numbers. | ||
- [`Subtract`](source/subtract.d.ts) - Returns the difference between two numbers. | ||
@@ -270,0 +279,0 @@ ### Change case |
@@ -1,2 +0,4 @@ | ||
import type {BuildTuple, Subtract, StaticPartOfArray, VariablePartOfArray, GTE} from './internal'; | ||
import type {BuildTuple, StaticPartOfArray, VariablePartOfArray} from './internal'; | ||
import type {GreaterThanOrEqual} from './greater-than-or-equal'; | ||
import type {Subtract} from './subtract'; | ||
import type {UnknownArray} from './unknown-array'; | ||
@@ -26,3 +28,3 @@ | ||
? [[], T] | ||
: GTE<StaticPartOfArray<T>['length'], SplitIndex> extends true | ||
: GreaterThanOrEqual<StaticPartOfArray<T>['length'], SplitIndex> extends true | ||
? [ | ||
@@ -29,0 +31,0 @@ SplitFixedArrayByIndex<StaticPartOfArray<T>, SplitIndex>[0], |
@@ -1,2 +0,3 @@ | ||
import type {BuildTuple, Subtract} from './internal'; | ||
import type {BuildTuple} from './internal'; | ||
import type {Subtract} from './subtract'; | ||
@@ -3,0 +4,0 @@ /** |
@@ -5,2 +5,5 @@ import type {Primitive} from './primitive'; | ||
import type {IsAny} from './is-any'; | ||
import type {NegativeInfinity, PositiveInfinity} from './numeric'; | ||
import type {GreaterThan} from './greater-than'; | ||
import type {LessThan} from './less-than'; | ||
import type {IsLiteral} from './is-literal'; | ||
@@ -56,3 +59,3 @@ import type {UnknownRecord} from './unknown-record'; | ||
*/ | ||
export type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = T extends {readonly length: L} | ||
export type BuildTuple<L extends number, Fill = unknown, T extends readonly unknown[] = []> = T['length'] extends L | ||
? T | ||
@@ -93,12 +96,2 @@ : BuildTuple<L, Fill, [...T, Fill]>; | ||
/** | ||
Create a tuple of length `A` and a tuple composed of two other tuples, | ||
the inferred tuple `U` and a tuple of length `B`, then extracts the length of tuple `U`. | ||
@link https://itnext.io/implementing-arithmetic-within-typescripts-type-system-a1ef140a6f6f | ||
*/ | ||
export type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...(infer U), ...BuildTuple<B>] | ||
? ArrayLength<U> | ||
: never; | ||
/** | ||
Matches any primitive, `void`, `Date`, or `RegExp` value. | ||
@@ -123,2 +116,104 @@ */ | ||
/** | ||
Converts a numeric string to a number. | ||
@example | ||
``` | ||
type PositiveInt = StringToNumber<'1234'>; | ||
//=> 1234 | ||
type NegativeInt = StringToNumber<'-1234'>; | ||
//=> -1234 | ||
type PositiveFloat = StringToNumber<'1234.56'>; | ||
//=> 1234.56 | ||
type NegativeFloat = StringToNumber<'-1234.56'>; | ||
//=> -1234.56 | ||
type PositiveInfinity = StringToNumber<'Infinity'>; | ||
//=> Infinity | ||
type NegativeInfinity = StringToNumber<'-Infinity'>; | ||
//=> -Infinity | ||
``` | ||
@category String | ||
@category Numeric | ||
@category Template literal | ||
*/ | ||
export type StringToNumber<S extends string> = S extends `${infer N extends number}` | ||
? N | ||
: S extends 'Infinity' | ||
? PositiveInfinity | ||
: S extends '-Infinity' | ||
? NegativeInfinity | ||
: never; | ||
/** | ||
Returns a boolean for whether the given string `S` starts with the given string `SearchString`. | ||
@example | ||
``` | ||
StartsWith<'abcde', 'abc'>; | ||
//=> true | ||
StartsWith<'abcde', 'bc'>; | ||
//=> false | ||
StartsWith<string, 'bc'>; | ||
//=> never | ||
StartsWith<'abcde', string>; | ||
//=> never | ||
``` | ||
@category String | ||
@category Template literal | ||
*/ | ||
export type StartsWith<S extends string, SearchString extends string> = string extends S | SearchString | ||
? never | ||
: S extends `${SearchString}${infer T}` | ||
? true | ||
: false; | ||
/** | ||
Returns the length of the given string. | ||
@example | ||
``` | ||
StringLength<'abcde'>; | ||
//=> 5 | ||
StringLength<string>; | ||
//=> never | ||
``` | ||
@category String | ||
@category Template literal | ||
*/ | ||
export type StringLength<S extends string> = string extends S | ||
? never | ||
: StringToArray<S>['length']; | ||
/** | ||
Returns an array of the characters of the string. | ||
@example | ||
``` | ||
StringToArray<'abcde'>; | ||
//=> ['a', 'b', 'c', 'd', 'e'] | ||
StringToArray<string>; | ||
//=> never | ||
``` | ||
@category String | ||
*/ | ||
export type StringToArray<S extends string, Result extends string[] = []> = string extends S | ||
? never | ||
: S extends `${infer F}${infer R}` | ||
? StringToArray<R, [...Result, F]> | ||
: Result; | ||
export type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'; | ||
@@ -367,2 +462,186 @@ | ||
/** | ||
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. | ||
@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. | ||
@example | ||
``` | ||
Not<true>; | ||
//=> false | ||
Not<false>; | ||
//=> true | ||
``` | ||
*/ | ||
export type Not<A extends boolean> = A extends true | ||
? false | ||
: A extends false | ||
? true | ||
: never; | ||
/** | ||
Returns the maximum value from a tuple of integers. | ||
Note: | ||
- Float numbers are not supported. | ||
@example | ||
``` | ||
ArrayMax<[1, 2, 5, 3]>; | ||
//=> 5 | ||
ArrayMax<[1, 2, 5, 3, 99, -1]>; | ||
//=> 99 | ||
``` | ||
*/ | ||
export type ArrayMax<A extends number[], Result extends number = NegativeInfinity> = number extends A[number] | ||
? never : | ||
A extends [infer F extends number, ...infer R extends number[]] | ||
? GreaterThan<F, Result> extends true | ||
? ArrayMax<R, F> | ||
: ArrayMax<R, Result> | ||
: Result; | ||
/** | ||
Returns the minimum value from a tuple of integers. | ||
Note: | ||
- Float numbers are not supported. | ||
@example | ||
``` | ||
ArrayMin<[1, 2, 5, 3]>; | ||
//=> 1 | ||
ArrayMin<[1, 2, 5, 3, -5]>; | ||
//=> -5 | ||
``` | ||
*/ | ||
export type ArrayMin<A extends number[], Result extends number = PositiveInfinity> = number extends A[number] | ||
? never | ||
: A extends [infer F extends number, ...infer R extends number[]] | ||
? LessThan<F, Result> extends true | ||
? ArrayMin<R, F> | ||
: ArrayMin<R, Result> | ||
: Result; | ||
/** | ||
Returns the absolute value of a given value. | ||
@example | ||
``` | ||
NumberAbsolute<-1>; | ||
//=> 1 | ||
NumberAbsolute<1>; | ||
//=> 1 | ||
NumberAbsolute<NegativeInfinity> | ||
//=> PositiveInfinity | ||
``` | ||
*/ | ||
export type NumberAbsolute<N extends number> = `${N}` extends `-${infer StringPositiveN}` ? StringToNumber<StringPositiveN> : N; | ||
/** | ||
Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both numeric strings and have the same length. | ||
@example | ||
``` | ||
SameLengthPositiveNumericStringGt<'50', '10'>; | ||
//=> true | ||
SameLengthPositiveNumericStringGt<'10', '10'>; | ||
//=> false | ||
``` | ||
*/ | ||
type SameLengthPositiveNumericStringGt<A extends string, B extends string> = A extends `${infer FirstA}${infer RestA}` | ||
? B extends `${infer FirstB}${infer RestB}` | ||
? FirstA extends FirstB | ||
? SameLengthPositiveNumericStringGt<RestA, RestB> | ||
: PositiveNumericCharacterGt<FirstA, FirstB> | ||
: never | ||
: false; | ||
type NumericString = '0123456789'; | ||
/** | ||
Returns a boolean for whether `A` is greater than `B`, where `A` and `B` are both positive numeric strings. | ||
@example | ||
``` | ||
PositiveNumericStringGt<'500', '1'>; | ||
//=> true | ||
PositiveNumericStringGt<'1', '1'>; | ||
//=> false | ||
PositiveNumericStringGt<'1', '500'>; | ||
//=> false | ||
``` | ||
*/ | ||
export type PositiveNumericStringGt<A extends string, B extends string> = A extends B | ||
? false | ||
: [BuildTuple<StringLength<A>, 0>, BuildTuple<StringLength<B>, 0>] extends infer R extends [readonly unknown[], readonly unknown[]] | ||
? R[0] extends [...R[1], ...infer Remain extends readonly unknown[]] | ||
? 0 extends Remain['length'] | ||
? SameLengthPositiveNumericStringGt<A, B> | ||
: true | ||
: false | ||
: never; | ||
/** | ||
Returns a boolean for whether `A` represents a number greater than `B`, where `A` and `B` are both positive numeric characters. | ||
@example | ||
``` | ||
PositiveNumericCharacterGt<'5', '1'>; | ||
//=> true | ||
PositiveNumericCharacterGt<'1', '1'>; | ||
//=> false | ||
``` | ||
*/ | ||
type PositiveNumericCharacterGt<A extends string, B extends string> = NumericString extends `${infer HeadA}${A}${infer TailA}` | ||
? NumericString extends `${infer HeadB}${B}${infer TailB}` | ||
? HeadA extends `${HeadB}${infer _}${infer __}` | ||
? true | ||
: false | ||
: never | ||
: never; | ||
/** | ||
Utility type to retrieve only literal keys from type. | ||
@@ -515,38 +794,2 @@ */ | ||
/** | ||
Returns the result of `A >= B`. | ||
@example | ||
``` | ||
type A = GTE<15, 10>; | ||
//=> true | ||
type B = GTE<10, 15>; | ||
//=> false | ||
type C = GTE<10, 10>; | ||
//=> true | ||
``` | ||
*/ | ||
export type GTE<A extends number, B extends number> = | ||
BuildTuple<A> extends [...infer _, ...BuildTuple<B>] | ||
? true | ||
: false; | ||
/** | ||
Returns the result of `A > B` | ||
@example | ||
``` | ||
type A = GT<15, 10>; | ||
//=> true | ||
type B = GT<10, 15>; | ||
//=> false | ||
*/ | ||
export type GT<A extends number, B extends number> = | ||
IsEqual<A, B> extends true | ||
? false | ||
: GTE<A, B>; | ||
/** | ||
Get the exact version of the given `Key` in the given object `T`. | ||
@@ -553,0 +796,0 @@ |
@@ -1,2 +0,2 @@ | ||
import type {Subtract} from './internal'; | ||
import type {Subtract} from './subtract'; | ||
import type {IsEqual} from './is-equal'; | ||
@@ -3,0 +3,0 @@ |
@@ -1,2 +0,2 @@ | ||
import type {Subtract} from './internal'; | ||
import type {Subtract} from './subtract'; | ||
import type {IsEqual} from './is-equal'; | ||
@@ -3,0 +3,0 @@ |
@@ -171,1 +171,18 @@ export type Numeric = number | bigint; | ||
export type NonNegativeInteger<T extends number> = NonNegative<Integer<T>>; | ||
/** | ||
Returns a boolean for whether the given number is a negative number. | ||
@see Negative | ||
@example | ||
``` | ||
import type {IsNegative} from 'type-fest'; | ||
type ShouldBeFalse = IsNegative<1>; | ||
type ShouldBeTrue = IsNegative<-1>; | ||
``` | ||
@category Numeric | ||
*/ | ||
export type IsNegative<T extends Numeric> = T extends Negative<T> ? true : false; |
348319
131
8363
968