@esfx/type-model Package
Provides a number of utility types for TypeScript.
Installation
npm i @esfx/type-model
Interfaces
AccessorPropertyDescriptor<T>
A PropertyDescriptor constrained to the valid attributes for an accessor.
MethodPropertyDescriptor<T>
A PropertyDescriptor constrained to the valid attributes for a method.
Type Aliases
AbstractConstructor
Represents an abstract class constructor.
Declaration
export declare type AbstractConstructor<T = {}, A extends any[] = any[]> = abstract new (...args: A) => T;
Type Parameters
- T
- A
And
Maps to true
if both A
and B
are true
; otherwise, false
.
Declaration
export declare type And<A extends boolean, B extends boolean> = IsNever<A> extends true ? never : IsNever<B> extends true ? never : A extends false ? false : B extends false ? false : true;
Type Parameters
- A
- B
AnyExclude
Gets a union of Exclude<T', U>
for each constituent T'
of T
.
Declaration
export declare type AnyExclude<T, U> = T extends unknown ? Exclude<T, U> : never;
Type Parameters
- T
- U
AnyExtract
Gets a union of Extract<T', U>
for each constituent T'
of T
.
Declaration
export declare type AnyExtract<T, U> = T extends unknown ? Extract<T, U> : never;
Type Parameters
- T
- U
AnyKeyof
Gets a union of keyof T'
of each constituent T'
of T
.
Declaration
export declare type AnyKeyof<T> = T extends unknown ? keyof T : never;
Type Parameters
- T
Assign
Combine the properties of A
and B
, chosing the properties in B
if the types differ.
Declaration
export declare type Assign<A extends object, B extends object> = Reshape<Diff<A, B> & B>;
Type Parameters
- A
- B
AsyncGeneratorNextType
Gets the type that can be sent to a generator via its next
method.
Declaration
export declare type AsyncGeneratorNextType<T> = T extends {
[Symbol.asyncIterator](): {
next(value?: infer U): any;
};
} ? U : never;
Type Parameters
- T
AsyncGeneratorReturnType
Gets the type that can be returned from a generator when it has finished executing.
Declaration
export declare type AsyncGeneratorReturnType<T> = T extends {
[Symbol.asyncIterator](): {
next(...args: any): PromiseLike<infer R>;
};
} ? R extends {
done?: boolean;
value?: any;
} ? R["done"] extends false | undefined ? never : Await<R["value"]> : never : never;
Type Parameters
- T
AsyncIteratedType
Gets the type yielded by an AsyncIterable.
Declaration
export declare type AsyncIteratedType<T> = T extends {
[Symbol.asyncIterator](): {
next(...args: any): PromiseLike<infer R>;
};
} ? R extends {
done?: boolean;
value?: any;
} ? R["done"] extends true ? never : Await<R["value"]> : never : never;
Type Parameters
- T
Await
Maps T
to its awaited type if T
is a promise.
Declaration
export declare type Await<T> = T extends {
then(onfulfilled: infer U): any;
} ? U extends ((value: infer V) => any) ? Await<V> : never : T;
Type Parameters
- T
AwaitAll
Maps each element of T
to its awaited type if the element is a promise.
Declaration
export declare type AwaitAll<T extends any[]> = {
[P in keyof T]: Await<T[P]>;
};
Type Parameters
- T
Conjoin
Joins a union of disjoint object types into a single object type.
Declaration
export declare type Conjoin<T extends object> = {
[P in AnyKeyof<T>]: AnyExtract<T, {
readonly [U in P]: unknown;
}>[P];
};
Type Parameters
- T
constnumber
A type useful as a base constraint for a number that should be inferred as a number literal type.
Declaration
export declare type constnumber = number & {} | 0;
Constructor
Represents a concrete ECMAScript constructor object.
Declaration
export declare type Constructor<T = {}, A extends any[] = any[]> = new (...args: A) => T;
Type Parameters
- T
- A
conststring
A type useful as a base constraint for a string that should be inferred as a string literal type.
Declaration
export declare type conststring = string & {} | "";
constsymbol
A type useful as a base constraint for a symbol that should be inferred as a unique symbol type.
Declaration
export declare type constsymbol = symbol & {} | typeof kIgnore;
consttuple
A type useful as a base constraint for an array that should be inferred as a tuple.
Declaration
export declare type consttuple<T> = readonly T[] | readonly [];
Type Parameters
- T
Diff
Remove from A
all properties with the same types that exist in B
.
Declaration
export declare type Diff<A extends object, B extends object> = Omit<A, keyof B>;
Type Parameters
- A
- B
Disjoin
Split an object into a union of objects for each key/value pair.
Declaration
export declare type Disjoin<T extends object> = IsNever<T> extends true ? never : IsAny<T> extends true ? any : __DisjoinRest<{
[K in keyof T]: {
[P in K]: T[P];
};
}[keyof T]>;
Type Parameters
- T
DisjoinOverlaps
Maps to true
if any type in A
is assignable to or shares a property with any type in B
; otherwise, false
.
This is similar to Overlaps
, except object types in A
and B
are mapped through Disjoin
.
Declaration
export declare type DisjoinOverlaps<A, B> = Overlaps<A extends object ? Disjoin<A> : A, B extends object ? Disjoin<B> : B>;
Type Parameters
- A
- B
Every
Maps to true
if every element of the tuple L
is true
; otherwise, false
.
Declaration
export declare type Every<L extends boolean[]> = L extends [] ? never : __EveryRest<{
[P in keyof L]: IsNever<L[P]> extends true ? "never" : IsAny<L[P]> extends true ? "boolean" : boolean extends L[P] ? "boolean" : L[P] extends false ? "false" : never;
}[number]>;
Type Parameters
- L
ExactType
Maps to true
if A
and B
are identical types; otherwise, false
.
Declaration
export declare type ExactType<A, B> = (<T>() => T extends A ? 0 : 1) extends (<T>() => T extends B ? 0 : 1) ? true : false;
Type Parameters
- A
- B
ExactTypes
Maps to true
if all elements of the tuple L
are identical; otherwise, false
.
Declaration
export declare type ExactTypes<L extends any[]> = L extends [] ? never : SameType<{
[P in keyof L]: ExactType<L[P], L[number]>;
}[number], true>;
Type Parameters
- L
Falsy
A union of all of the falsy types in TypeScript.
Declaration
export declare type Falsy = null | undefined | false | 0 | 0n | '';
FunctionKeys
Maps to the keys of T
whose values are functions.
Declaration
export declare type FunctionKeys<T, F extends Function = Function> = MatchingKeys<T, F>;
Type Parameters
- T
- F
GeneratorNextType
Gets the type that can be sent to a generator via its next
method.
Declaration
export declare type GeneratorNextType<T> = T extends {
[Symbol.iterator](): {
next(value?: infer U): any;
};
} ? U : never;
Type Parameters
- T
GeneratorReturnType
Gets the type that can be returned from a generator when it has finished executing.
Declaration
export declare type GeneratorReturnType<T> = T extends {
[Symbol.iterator](): {
next(...args: any): infer R;
};
} ? R extends {
done?: boolean;
value: any;
} ? R["done"] extends false | undefined ? never : R["value"] : never : never;
Type Parameters
- T
Intersect
Pick from A
all properties with the same types that exist in B
.
Declaration
export declare type Intersect<A extends object, B extends object> = Pick<A & B, Extract<keyof A, keyof B> & Extract<keyof B, keyof A>>;
Type Parameters
- A
- B
Intersection
Maps an ordered tuple of types into an intersection of those types.
Declaration
export declare type Intersection<A extends any[]> = A extends [infer H, ...infer T] ? H & Intersection<T> : unknown;
Type Parameters
- A
Is
Constrains T
to U
.
Declaration
export declare type Is<T extends U, U> = T;
Type Parameters
- T
- U
IsAny
Maps to true
if A
is precisely the any
type; otherwise, false
.
Declaration
export declare type IsAny<A> = boolean extends (A extends never ? true : false) ? true : false;
Type Parameters
- A
IsCallable
Maps to true
if the type has a call signature; otherwise, false
.
Declaration
export declare type IsCallable<T> = IsAny<T> extends true ? boolean : IsNever<T> extends true ? never : SameType<T, Function> extends true ? true : [
T
] extends [(...args: any) => any] ? true : false;
Type Parameters
- T
IsConstructable
Maps to true
if the type has a construct signature; otherwise, false
.
Declaration
export declare type IsConstructable<T> = IsAny<T> extends true ? boolean : IsNever<T> extends true ? never : SameType<T, Function> extends true ? true : [
T
] extends [new (...args: any) => any] ? true : false;
Type Parameters
- T
IsEmpty
Maps to true
if T
is an empty object ({}
).
Declaration
export declare type IsEmpty<T extends object> = IsNever<keyof T>;
Type Parameters
- T
IsNever
Maps to true
if A
is precisely the never
type; otherwise, false
.
Declaration
export declare type IsNever<A> = [A] extends [never] ? true : false;
Type Parameters
- A
IsProperSubsetOf
Maps to true
if Sub
is a proper subset of Super
; otherwise, false
.
Declaration
export declare type IsProperSubsetOf<Sub, Super> = IsAny<Sub> extends true ? boolean : IsAny<Super> extends true ? boolean : SameType<Sub, Super> extends true ? false : __IsSubsetOf<Sub, Super>;
Type Parameters
- Sub
- Super
IsProperSupersetOf
Maps to true
if Super
is a proper superset of Sub
; otherwise, false
.
Declaration
export declare type IsProperSupersetOf<Super, Sub> = IsProperSubsetOf<Sub, Super>;
Type Parameters
- Super
- Sub
IsSubsetOf
Maps to true
if Sub
is a subset of Super
; otherwise, false
.
Declaration
export declare type IsSubsetOf<Sub, Super> = IsAny<Sub> extends true ? boolean : IsAny<Super> extends true ? boolean : __IsSubsetOf<Sub, Super>;
Type Parameters
- Sub
- Super
IsSubtypeOf
Maps to true
if Sub
is a subtype of Super
; otherwise, false
.
Declaration
export declare type IsSubtypeOf<Sub, Super> = IsNever<Super> extends true ? IsNever<Sub> : IsNever<Sub> extends true ? true : IsAny<Super> extends true ? true : IsAny<Sub> extends true ? true : Sub extends Super ? true : false;
Type Parameters
- Sub
- Super
IsSupersetOf
Maps to true
if Super
is a superset of Sub
; otherwise, false
.
Declaration
export declare type IsSupersetOf<Super, Sub> = IsSubsetOf<Sub, Super>;
Type Parameters
- Super
- Sub
IsSupertypeOf
Maps to true
if Super
is a supertype of Sub
; otherwise, false
.
Declaration
export declare type IsSupertypeOf<Super, Sub> = IsSubtypeOf<Sub, Super>;
Type Parameters
- Super
- Sub
IsUnion
Maps to true
if T
is a union of multiple types; otherwise, false
.
Declaration
export declare type IsUnion<T> = IsNever<T> extends true ? false : __IsUnionRest<T, [T]>;
Type Parameters
- T
IsUnknown
Maps to true
if A
is precisely the unknown
type; otherwise, false
.
Declaration
export declare type IsUnknown<A> = IsAny<A> extends true ? false : unknown extends A ? true : false;
Type Parameters
- A
IteratedType
Gets the type yielded by an Iterable.
Declaration
export declare type IteratedType<T> = T extends {
[Symbol.iterator](): {
next(...args: any): infer R;
};
} ? R extends {
done?: boolean;
value: any;
} ? R["done"] extends true ? never : R["value"] : never : never;
Type Parameters
- T
MatchingKeys
Maps to the keys of T
whose values match TMatch
.
Declaration
export declare type MatchingKeys<T, TMatch> = {
[P in keyof T]: T[P] extends TMatch ? P : never;
}[keyof T];
Type Parameters
- T
- TMatch
Mutable
Maps to a mutable copy of T.
Declaration
export declare type Mutable<T> = {
-readonly [P in keyof T]: T[P];
};
Type Parameters
- T
NonFunctionKeys
Maps to the keys of T
whose values are not functions.
Declaration
export declare type NonFunctionKeys<T, F extends Function = Function> = NonMatchingKeys<T, F>;
Type Parameters
- T
- F
NonMatchingKeys
Maps to the keys of T
whose values do not match TMatch
.
Declaration
export declare type NonMatchingKeys<T, TMatch> = Exclude<keyof T, MatchingKeys<T, TMatch>>;
Type Parameters
- T
- TMatch
NonOptional
Strips undefined
from a type.
Declaration
export declare type NonOptional<T> = T extends undefined ? never : T;
Type Parameters
- T
Not
Maps to true
if A
is false
, otherwise true
.
Declaration
export declare type Not<A extends boolean> = IsNever<A> extends true ? never : A extends false ? true : false;
Type Parameters
- A
Nullable
Indicates a type that may be null
or undefined
.
Declaration
export declare type Nullable<T> = T | undefined | null;
Type Parameters
- T
numstr
Gets a union of the number and numeric string value for a number or numeric string index between 0 and 255
Declaration
export declare type numstr<I extends keyof any> = _SupportsNumericExtraction extends true ? _numstrUsingNumericExtraction<I> : _numstrUsingTable<I>;
Type Parameters
- I
One
Maps to true
if exactly one element of the tuple L
is true
; otherwise, false
.
Declaration
export declare type One<L extends boolean[]> = L extends [] ? never : __OneRest<{
[P in keyof L]: IsNever<L[P]> extends true ? "never" : IsAny<L[P]> extends true ? "boolean" : boolean extends L[P] ? "boolean" : L[P] extends true ? [P] : never;
}[number]>;
Type Parameters
- L
Optional
Indicates a type that may be undefined
.
Declaration
export declare type Optional<T> = T | undefined;
Type Parameters
- T
OptionalKeyof
Warning
Deprecated
Use `OptionalKeysDeclaration
export declare type OptionalKeyof<T> = OptionalKeys<T>;
Type Parameters
- T
OptionalKeys
Gets a union of the keys of T
that are optional.
Declaration
export declare type OptionalKeys<T> = Exclude<keyof T, RequiredKeys<T>>;
Type Parameters
- T
Or
Maps to true
if either A
or B
are true
; otherwise, false
.
Declaration
export declare type Or<A extends boolean, B extends boolean> = IsNever<A> extends true ? never : IsNever<B> extends true ? never : A extends true ? true : B extends true ? true : false;
Type Parameters
- A
- B
Overlaps
Maps to true
if any type in A
is assignable to any type in B
; otherwise, false
.
Declaration
export declare type Overlaps<A, B> = IsNever<A> extends true ? false : IsNever<B> extends true ? false : IsAny<A> extends true ? true : IsAny<B> extends true ? true : 1 extends (A extends unknown ? A extends B ? 1 : 2 : 3) ? true : 1 extends (B extends unknown ? B extends A ? 1 : 2 : 3) ? true : false;
Type Parameters
- A
- B
Pop
Maps to a tuple where the first element is the last element of L
and the second element are the remaining elements of L
.
Declaration
export declare type Pop<L extends any[]> = L extends [...infer H, infer T] ? [T, H] : [never, never];
Type Parameters
- L
Primitive
A union of all of the primitive types in TypeScript.
Declaration
export declare type Primitive = string | symbol | boolean | number | bigint;
PromisedType
Gets the promised type of a Promise.
Declaration
export declare type PromisedType<T> = T extends {
then(onfulfilled: infer U): any;
} ? U extends ((value: infer V) => any) ? V : never : never;
Type Parameters
- T
Push
Push an element on to the end of a tuple.
Declaration
export declare type Push<H extends any[], T> = [...H, T];
Type Parameters
- H
- T
Relatable
Maps to true
if either A
or B
are relatable to each other.
Declaration
export declare type Relatable<A, B> = IsNever<A> extends true ? false : IsNever<B> extends true ? false : IsAny<A> extends true ? true : IsAny<B> extends true ? true : [
A
] extends [B] ? true : [
B
] extends [A] ? true : false;
Type Parameters
- A
- B
RequiredKeyof
Warning
Deprecated
Use `RequiredKeysDeclaration
export declare type RequiredKeyof<T> = RequiredKeys<T>;
Type Parameters
- T
RequiredKeys
Gets a union of the keys of T
that are non-optional.
Declaration
export declare type RequiredKeys<T> = _RequiredKeyof<T, keyof T>;
Type Parameters
- T
Reshape
Map an intersection of object types into a single object type.
Declaration
export declare type Reshape<T extends object> = Pick<T, keyof T>;
Type Parameters
- T
Reverse
Reverse the order of the elements of a tuple.
Declaration
export declare type Reverse<L extends any[]> = L extends [infer H, ...infer T] ? [...Reverse<T>, H] : [];
Type Parameters
- L
SameType
Maps to true
if both A
and B
are assignable to each other; otherwise, false
.
Declaration
export declare type SameType<A, B> = IsNever<A> extends true ? IsNever<B> : IsNever<B> extends true ? false : [
A,
B
] extends [B, A] ? true : false;
Type Parameters
- A
- B
SameTypes
Maps to true
if all elements of the tuple L
are assignable to each other; otherwise, false
.
Declaration
export declare type SameTypes<L extends any[]> = L extends [] ? never : SameType<{
[P in keyof L]: SameType<L[P], L[number]>;
}[number], true>;
Type Parameters
- L
Shift
Maps to a tuple where the first element is the first element of L
and the second element are the remaining elements of L
.
Declaration
export declare type Shift<L extends any[]> = L extends [infer H, ...infer T] ? [H, T] : [never, never];
Type Parameters
- L
Some
Maps to true
if any element of the tuple L
is true
; otherwise, false
.
Declaration
export declare type Some<L extends boolean[]> = L extends [] ? never : __SomeRest<{
[P in keyof L]: IsNever<L[P]> extends true ? "never" : IsAny<L[P]> extends true ? "boolean" : boolean extends L[P] ? "boolean" : L[P] extends true ? "true" : never;
}[number]>;
Type Parameters
- L
Union
Maps an ordered tuple of types into a union of those types.
Declaration
export declare type Union<A extends any[]> = A[number];
Type Parameters
- A
Unshift
Inserts an element at the start of a tuple.
Declaration
export declare type Unshift<T extends any[], H> = [H, ...T];
Type Parameters
- T
- H
XOr
Maps to true
if only one of either A
or B
are true
; otherwise, false
.
Declaration
export declare type XOr<A extends boolean, B extends boolean> = IsNever<A> extends true ? never : IsNever<B> extends true ? never : A extends true ? Not<B> : B extends true ? Not<A> : false;
Type Parameters
- A
- B