type-fest
Advanced tools
Comparing version 4.8.3 to 4.9.0
@@ -108,2 +108,3 @@ // Basic | ||
export type {Paths} from './source/paths'; | ||
export type {SharedUnionFieldsDeep} from './source/shared-union-fields-deep'; | ||
@@ -110,0 +111,0 @@ // Template literal types |
{ | ||
"name": "type-fest", | ||
"version": "4.8.3", | ||
"version": "4.9.0", | ||
"description": "A collection of essential TypeScript types", | ||
@@ -5,0 +5,0 @@ "license": "(MIT OR CC0-1.0)", |
@@ -182,2 +182,3 @@ <div align="center"> | ||
- [`Paths`](source/paths.d.ts) - Generate a union of all possible paths to properties in the given object. | ||
- [`SharedUnionFieldsDeep`](source/shared-union-fields-deep.d.ts) - Create a type with shared fields from a union of object types, deeply traversing nested structures. | ||
@@ -184,0 +185,0 @@ ### Type Guard |
@@ -6,2 +6,3 @@ import type {IsEqual} from './is-equal'; | ||
import type {EmptyObject} from './empty-object'; | ||
import type {IsPlainObject} from './internal'; | ||
@@ -101,5 +102,5 @@ /** | ||
? Type[Key] | ||
: Type[Key] extends UnknownRecord | ||
: IsPlainObject<Type[Key]> extends true | ||
? ConditionalPickDeep<Type[Key], Condition, Options> | ||
: typeof conditionalPickDeepSymbol; | ||
}, (typeof conditionalPickDeepSymbol | undefined) | EmptyObject>, never, UnknownRecord>; |
@@ -6,2 +6,4 @@ import type {Primitive} from './primitive'; | ||
import type {UnknownRecord} from './unknown-record'; | ||
import type {IsNever} from './is-never'; | ||
import type {UnknownArray} from './unknown-array'; | ||
@@ -16,5 +18,32 @@ // TODO: Remove for v5. | ||
*/ | ||
type TupleLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never; | ||
type ArrayLength<T extends readonly unknown[]> = T extends {readonly length: infer L} ? L : never; | ||
/** | ||
Infer the length of the given tuple `<T>`. | ||
Returns `never` if the given type is an non-fixed-length array like `Array<string>`. | ||
@example | ||
``` | ||
type Tuple = TupleLength<[string, number, boolean]>; | ||
//=> 3 | ||
type Array = TupleLength<string[]>; | ||
//=> never | ||
// Supports union types. | ||
type Union = TupleLength<[] | [1, 2, 3] | Array<number>>; | ||
//=> 1 | 3 | ||
``` | ||
*/ | ||
export type TupleLength<T extends UnknownArray> = | ||
// `extends unknown` is used to convert `T` (if `T` is a union type) to | ||
// a [distributive conditionaltype](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types)) | ||
T extends unknown | ||
? number extends T['length'] | ||
? never // Return never if the given type is an non-flexed-length array like `Array<string>` | ||
: T['length'] | ||
: never; // Should never happen | ||
/** | ||
Create a tuple type of the given length `<L>` and fill it with the given type `<Fill>`. | ||
@@ -68,9 +97,9 @@ | ||
export type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...(infer U), ...BuildTuple<B>] | ||
? TupleLength<U> | ||
? ArrayLength<U> | ||
: never; | ||
/** | ||
Matches any primitive, `Date`, or `RegExp` value. | ||
Matches any primitive, `void`, `Date`, or `RegExp` value. | ||
*/ | ||
export type BuiltIns = Primitive | Date | RegExp; | ||
export type BuiltIns = Primitive | void | Date | RegExp; | ||
@@ -82,2 +111,12 @@ /** | ||
/** | ||
Returns a boolean for whether the given type is a plain key-value object. | ||
*/ | ||
export type IsPlainObject<T> = | ||
T extends NonRecursiveType | UnknownArray | ReadonlyMap<unknown, unknown> | ReadonlySet<unknown> | ||
? false | ||
: T extends object | ||
? true | ||
: false; | ||
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'; | ||
@@ -324,1 +363,81 @@ | ||
export type IsPrimitive<T> = [T] extends [Primitive] ? true : false; | ||
/** | ||
Returns the static, fixed-length portion of the given array, excluding variable-length parts. | ||
@example | ||
``` | ||
type A = [string, number, boolean, ...string[]]; | ||
type B = StaticPartOfArray<A>; | ||
//=> [string, number, boolean] | ||
``` | ||
*/ | ||
export type StaticPartOfArray<T extends UnknownArray, Result extends UnknownArray = []> = | ||
T extends unknown | ||
? number extends T['length'] ? | ||
T extends readonly [infer U, ...infer V] | ||
? StaticPartOfArray<V, [...Result, U]> | ||
: Result | ||
: T | ||
: never; // Should never happen | ||
/** | ||
Returns the variable, non-fixed-length portion of the given array, excluding static-length parts. | ||
@example | ||
``` | ||
type A = [string, number, boolean, ...string[]]; | ||
type B = VariablePartOfArray<A>; | ||
//=> string[] | ||
``` | ||
*/ | ||
export type VariablePartOfArray<T extends UnknownArray> = | ||
T extends unknown | ||
? T extends readonly [...StaticPartOfArray<T>, ...infer U] | ||
? U | ||
: [] | ||
: never; // Should never happen | ||
/** | ||
Returns the minimum number in the given union of numbers. | ||
Note: Just supports numbers from 0 to 999. | ||
@example | ||
``` | ||
type A = UnionMin<3 | 1 | 2>; | ||
//=> 1 | ||
``` | ||
*/ | ||
export type UnionMin<N extends number> = InternalUnionMin<N>; | ||
/** | ||
The actual implementation of `UnionMin`. It's private because it has some arguments that don't need to be exposed. | ||
*/ | ||
type InternalUnionMin<N extends number, T extends UnknownArray = []> = | ||
T['length'] extends N | ||
? T['length'] | ||
: InternalUnionMin<N, [...T, unknown]>; | ||
/** | ||
Returns the maximum number in the given union of numbers. | ||
Note: Just supports numbers from 0 to 999. | ||
@example | ||
``` | ||
type A = UnionMax<1 | 3 | 2>; | ||
//=> 3 | ||
``` | ||
*/ | ||
export type UnionMax<N extends number> = InternalUnionMax<N>; | ||
/** | ||
The actual implementation of `UnionMax`. It's private because it has some arguments that don't need to be exposed. | ||
*/ | ||
type InternalUnionMax<N extends number, T extends UnknownArray = []> = | ||
IsNever<N> extends true | ||
? T['length'] | ||
: T['length'] extends N | ||
? InternalUnionMax<Exclude<N, T['length']>, T> | ||
: InternalUnionMax<N, [...T, unknown]>; |
@@ -1,2 +0,2 @@ | ||
import type {NonRecursiveType, ToString} from './internal'; | ||
import type {StaticPartOfArray, VariablePartOfArray, NonRecursiveType, ToString} from './internal'; | ||
import type {EmptyObject} from './empty-object'; | ||
@@ -8,34 +8,2 @@ import type {IsAny} from './is-any'; | ||
/** | ||
Return the part of the given array with a fixed index. | ||
@example | ||
``` | ||
type A = [string, number, boolean, ...string[]]; | ||
type B = FilterFixedIndexArray<A>; | ||
//=> [string, number, boolean] | ||
``` | ||
*/ | ||
type FilterFixedIndexArray<T extends UnknownArray, Result extends UnknownArray = []> = | ||
number extends T['length'] ? | ||
T extends readonly [infer U, ...infer V] | ||
? FilterFixedIndexArray<V, [...Result, U]> | ||
: Result | ||
: T; | ||
/** | ||
Return the part of the given array with a non-fixed index. | ||
@example | ||
``` | ||
type A = [string, number, boolean, ...string[]]; | ||
type B = FilterNotFixedIndexArray<A>; | ||
//=> string[] | ||
``` | ||
*/ | ||
type FilterNotFixedIndexArray<T extends UnknownArray> = | ||
T extends readonly [...FilterFixedIndexArray<T>, ...infer U] | ||
? U | ||
: []; | ||
/** | ||
Generate a union of all possible paths to properties in the given object. | ||
@@ -89,4 +57,4 @@ | ||
// We need to handle the fixed and non-fixed index part of the array separately. | ||
? InternalPaths<FilterFixedIndexArray<T>> | ||
| InternalPaths<Array<FilterNotFixedIndexArray<T>[number]>> | ||
? InternalPaths<StaticPartOfArray<T>> | ||
| InternalPaths<Array<VariablePartOfArray<T>[number]>> | ||
: InternalPaths<T> | ||
@@ -93,0 +61,0 @@ : T extends object |
318866
121
7475
957