-
Notifications
You must be signed in to change notification settings - Fork 0
Types
General-Purpose TypeScript Helper Types.
type BaseType <TypeStringT extends BaseTypeString = never> = string | number | bigint | boolean | symbol | object | any[] | ((...args: any[]) => any) | null | undefined;
The Base Types from which all types are derived.
These types are a superset of the types recognized by the typeof
operator.
The string
-based variant of this type is BaseTypeString.
-
TypeStringT
: An optional BaseTypeString or union ofBaseTypeString
members used to determine which Base Types should be returned.
If omitted or never
, a union of all of the Base Types will be returned.
A union of Base Types.
If TypeStringT
contains a BaseTypeString or union of BaseTypeString
members, a union containing all of the corresponding Base Types will be returned.
If TypeStringT
is never
or the entire BaseTypeString union, a union of all of the Base Types will be returned.
type BaseTypeString <TypeT extends BaseType = any> = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null" | "array";
The string
values corresponding to each of the Base Types from which all types are derived.
This is the string
-based variant of the BaseType type.
-
TypeT
: An optional BaseType or union ofBaseType
members used to determine which Base Types should be returned.
If omitted or any
, a union of all of the Base Type string
members will be returned.
A union of Base Type string
members.
If TypeT
contains a BaseType or union of BaseType
members, a union containing all of the corresponding Base Type string
members will be returned.
If TypeT
is never
or the entire BaseType union, a union of all of the Base Type string
members will be returned.
function getBaseType <T extends BaseType> ( x: T ): BaseTypeString<T>;
// function getBaseType ( x: BaseType ): BaseTypeString;
Get the BaseType of the given value.
-
T
: The type of the specified value.
-
x
: The value being evaluated.
A BaseTypeString corresponding to the BaseType of x
.
function isBaseType <TypesT extends BaseTypeString | BaseTypeString[]> (
x: BaseType,
types: TypesT
): x is BaseType<TypesT extends BaseTypeString[] ? TypesT[number] : TypesT>;
// function getBaseType ( x: BaseType, types: BaseTypeString | BaseTypeString[] ): boolean;
Check if the given value is of any of the specified BaseTypes.
-
TypesT
: The type of thetypes
argument.
-
x
: The value being evaluated. -
types
: The BaseTypeString or an array ofBaseTypeString
s corresponding to the BaseType(s) to check for.
true
if x
is of the specified BaseType(s) or false
if it is not.
function assertBaseType <TypesT extends BaseTypeString | BaseTypeString[]> (
x: BaseType,
types: TypesT
): x is BaseType<TypesT extends BaseTypeString[] ? TypesT[number] : TypesT>;
// function assertBaseType ( x: BaseType, types: BaseTypeString | BaseTypeString[] ): true;
Assert the given value is of any of the specified BaseTypes and throw a TypeError if it is not.
-
TypesT
: The type of thetypes
argument.
-
x
: The value being evaluated. -
types
: The BaseTypeString or an array ofBaseTypeString
s corresponding to the BaseType(s) to check for.
true
if x
is of the specified BaseType(s).
A TypeError if x
is not of the specified BaseType(s).
type IsAny <T> = boolean;
Determine if the given type is any
.
-
T
: The type being evaluated.
true
if T
is any
and false
otherwise.
type UnionToIntersection <U> = any;
Transform the specified union into an intersection.
-
U
: The union being transformed into an intersection.
An intersection of the union members of U
.
If U
is not a union or is a generic type such as any
, returns U
.
If the intersection of the union members of U
produces an impossible combination (e.g., true & false
), returns never
.