Options
All
  • Public
  • Public/Protected
  • All
Menu

@vangware/types

Index

Array Type Aliases

Empty<ArrayRecordOrString>: ArrayRecordOrString extends ReadOnlyArray ? EmptyArray : ArrayRecordOrString extends ReadOnlyRecord ? EmptyRecord : ArrayRecordOrString extends string ? EmptyString : never

Empty array, object or string.

example
const emptyString: Empty<string> = "";
const emptyArray: Empty<ReadOnlyArray> = [];
const emptyRecord: Empty<ReadOnlyRecord> = {};

Type Parameters

EmptyArray: readonly []

Empty array.

example
const emptyArray: EmptyArray = [];
Entries<Key, Value>: ReadonlyArray<Entry<Key, Value>>

Array of Entry.

example
const entries: Entries<string, number> = [
["🟢", 1],
["🟩", 2],
];

Type Parameters

  • Key = PropertyKey

  • Value = unknown

EntriesOf<ArrayOrRecord>: Entries<KeyOf<ArrayOrRecord>, ValueOf<ArrayOrRecord>>

Array of EntryOf.

example
const object = {
"🟢": 1,
"🟩": 2,
};
const entries: EntriesOf<typeof object> = Object.entries(object);

Type Parameters

Entry<Key, Value>: readonly [key: Key, value: Value]

Entry couple [key, value].

example
const entry: Entry<string, number> = ["🟢", 1];

Type Parameters

  • Key = PropertyKey

  • Value = unknown

EntryKey<Input>: Input[0]

Key of an Entry.

example
const entry: Entry<string, number> = ["🟢", 1];
const entryKey: EntryKey<typeof entry> = entry[0];

Type Parameters

EntryOf<ArrayOrRecord>: Entry<KeyOf<ArrayOrRecord>, ValueOf<ArrayOrRecord>>

Object or array Entry.

example
const object = {
"🟢": 1,
"🟩": 2,
};
const entries: EntryOf<typeof object> = Object.entries(object)[0];

Type Parameters

EntryValue<Input>: Input[1]

Value of an Entry.

example
const entry: Entry<string, number> = ["🟢", 1];
const entryValue: EntryValue<typeof entry> = entry[1];

Type Parameters

Head<Input>: HeadAndTail<Input>[0]

Initial value of an array or string.

example
const array = ["🟢", "🟩", "💚"];
const head: Head<typeof array> = "🟢";

Type Parameters

  • Input extends ArrayLike<unknown>

HeadAndTail<Input>: Input extends readonly [infer Head, ...(infer Tail)] ? readonly [Head, Tail] : Input extends `${infer Head}${infer Tail}` ? readonly [Head, Tail] : Input extends EmptyArray | EmptyString ? readonly [undefined, Input] : readonly [Input[number] | undefined, Input]

Get a couple with the head and tail of an array or string.

example
const array = ["🟢", "🟩", "💚"];
const headAndTail: HeadAndTail<typeof array> = ["🟢", ["🟩", "💚"]];

Type Parameters

  • Input extends ArrayLike<unknown>

Initial<Input>: InitialAndLast<Input>[0]

Initial values of an array or string (omitting the last).

example
const array = ["🟢", "🟩", "💚"];
const initial: Initial<typeof array> = ["🟢", "🟩"];

Type Parameters

  • Input extends ArrayLike<unknown>

InitialAndLast<Input>: Input extends readonly [...(infer Initial), infer Last] ? readonly [Initial, Last] : Input extends EmptyArray | EmptyString ? readonly [Input, undefined] : Input extends `${infer Initial}${infer Rest}` ? readonly [`${Rest extends EmptyString ? EmptyString : Initial}${InitialAndLast<Rest>[0]}`, `${Rest extends EmptyString ? Initial : InitialAndLast<Rest>[1]}`] : readonly [Input, Input[number] | undefined]

Get a couple with the initial and last values of an array or string.

example
const array = ["🟢", "🟩", "💚"];
const initialAndLast: InitialAndLast<typeof array> = [["🟢", "🟩"], "💚"];

Type Parameters

  • Input extends ArrayLike<unknown>

KeyOf<ArrayOrRecord>: ArrayOrRecord extends ReadOnlyArray ? KeyOfArray<ArrayOrRecord> : keyof ArrayOrRecord extends Exclude<PropertyKey, symbol> ? `${keyof ArrayOrRecord}` : never

Generic key for either object or array.

Type Parameters

KeyOfArray<Input>: NeverFallback<Exclude<Partial<Input>["length"], Input["length"]>, number>

Get type of index of array (can be a const number or just a number).

example
const lengthConst: Length<[1, 2, 3]> = 2; // Can be: 0 | 1 | 2
const lengthUnknown: Length<ReadOnlyArray<number>> = 10; // Can be any number

Type Parameters

  • Input extends ArrayLike<unknown>

Last<Input>: InitialAndLast<Input>[1]

Last value of an array or string.

example
const array = ["🟢", "🟩", "💚"];
const last: Last<typeof array> = "💚";

Type Parameters

  • Input extends ArrayLike<unknown>

NotEmpty<Type>: Not<Empty<Type>, Type>

Not empty array, object or string.

example
const notEmptyString: NotEmpty<string> = "🟢";
const notEmptyArray: NotEmpty<ReadOnlyArray> = ["🟢", "🟩"];
const notEmptyRecord: NotEmpty<ReadOnlyRecord> = { "🟢": "🟩" };

Type Parameters

ReadOnlyArray<Item>: ReadonlyArray<Item>

Read-only array.

example
const array: ReadOnlyArray<number> = [1, 2, 3];

Type Parameters

  • Item = unknown

Reducer<Item, Accumulator>: Unary<Item, Unary<Accumulator, Accumulator>>

Reducer/Folder function.

example
const array: ReadOnlyArray<number> = [1, 2, 3];
const reducer: Reducer<number, number> = item => accumulator => accumulator + item;

Type Parameters

  • Item

  • Accumulator

Sorter<Item>: Unary<Item, Unary<Item, number>>

Sorter function.

example
const array: ReadOnlyArray<number> = [1, 2, 3];
const sorter: Sorter<number> = value1 => value2 => value1 - value2;

Type Parameters

  • Item

Tail<Input>: HeadAndTail<Input>[1]

Last values of an array or string (omitting the first).

example
const array = ["🟢", "🟩", "💚"];
const tail: Tail<typeof array> = ["🟩", "💚"];

Type Parameters

  • Input extends ArrayLike<unknown>

ValueOf<ArrayOrObject>: ArrayOrObject[KeyOf<ArrayOrObject>]

Generic key for either object or array.

example
const object: ReadOnlyRecord<number> = {
"🟢": 1,
"🟩": 2,
};
const key: ValueOf<typeof object> = 1;

Type Parameters

Common Type Aliases

Falsy: EmptyString | 0 | 0n | 0 | 0n | false | undefined

Types that evaluates to false in JS.

example
const falsyString: Falsy = "";
const falsyNegativeZero: Falsy = -0;
const falsyNegativeZeroBigInt: Falsy = -0n;
const falsyZero: Falsy = 0;
const falsyZeroBigInt: Falsy = 0n;
const falsyBoolean: Falsy = false;
const falsyUndefined: Falsy = undefined;
Maybe<Value>: Value | undefined

Value that can be undefined.

example
type MaybeNumber = Maybe<number>;
const maybeNumber: MaybeNumber = 1;
const notNumber: MaybeNumber = undefined;

Type Parameters

  • Value

MaybePromise<Value>: Promise<Value> | Value

A value that might be coming from a Promise.

example
type MaybeStringPromise = MaybePromise<string>;
const promisedValue: MaybeStringPromise = Promise.resolve("🟢");
const plainValue: MaybeStringPromise = "🟩";

Promise.all([promisedValue, plainValue]).then(console.log); // ["🟢", "🟩"]

Type Parameters

  • Value

NeverFallback<MaybeNever, Fallback>: Single<MaybeNever> extends Single<never> ? Fallback : MaybeNever

Takes a value that could be never, and if it is never it goes to the Fallback value.

example
const value: never = (undefined as never);
NeverFallback<typeof value, number>; // Will be number

Type Parameters

  • MaybeNever

  • Fallback

Not<Negated, Type>: Type extends Negated ? never : Type

Negates a type.

example
type NotNumber<Type> = Not<number, Type>;
const notNumber: NotNumber<string | number> = "🟢";

Type Parameters

  • Negated

  • Type

Primitive: bigint | boolean | number | string | symbol | undefined

Valid JavaScript primitives (excluding null).

example
const aBigInt: Primitive = 13n;
const aBoolean: Primitive = true;
const aNumber: Primitive = 13;
const aString: Primitive = "🟢";
const aSymbol: Primitive = Symbol("🟢");
const anUndefined: Primitive = undefined;
ReplaceType<Input, Keys, NewType>: Omit<Input, Keys> & ReadOnlyRecord<NewType, Keys>

Replaces the type of some keys in given Input.

example
type User = { name: string; age: number };
type ReallyOldUser = ReplaceType<User, "age", bigint>;

Type Parameters

  • Input

  • Keys extends keyof Input

  • NewType

Single<Type>: readonly [Type]

Tuple of length 1 (AKA Monuple).

example
const single: Single<number> = [1];

Type Parameters

  • Type

Truthy<Actual>: Not<Falsy, Actual>

Generic type to check if value is not Falsy.

example
Truthy<"" | "truthy">; // "truthy"

Type Parameters

  • Actual = unknown

TypeOfMap: { bigint: bigint; boolean: boolean; function: GenericFunction; number: number; object: ReadOnlyRecord; string: string; symbol: symbol; undefined: undefined }

typeof type map.

example
TypeOfMap["string"]; // `string`
TypeOfMap["boolean"]; // `boolean`
TypeOfMap["function"]; // `GenericFunction`

Type declaration

  • Readonly bigint: bigint
  • Readonly boolean: boolean
  • Readonly function: GenericFunction
  • Readonly number: number
  • Readonly object: ReadOnlyRecord
  • Readonly string: string
  • Readonly symbol: symbol
  • Readonly undefined: undefined
TypeOfValue: keyof TypeOfMap

Possible type values returned by typeof.

example
const typeString: TyeOfValue = "string";
const typeBoolean: TyeOfValue = "boolean";
const typeFunction: TyeOfValue = "function";

Date Type Aliases

ISODate: `${number}-${ISOMonth}-${ISODay}T${number}:${number}:${number}.${number}Z`

String representing an ISO date.

example
const date: ISODate = "2020-01-01T00:00:00.000Z";
ISODay: `0${Exclude<Digit, 0>}` | `1${Digit}` | `2${Digit}` | `3${0 | 1}`

ISO day value ("01" to "31").

example
const day01: ISODay = "01";
const day15: ISODay = "15";
const day31: ISODay = "31";
ISOMonth: `0${Exclude<Digit, 0>}` | `1${0 | 1 | 2}`

ISO month value ("01" to "12").

example
const month01: ISOMonth = "01";
const month06: ISOMonth = "06";
const month12: ISOMonth = "12";

Function Type Aliases

Comparison<Right, Left, Predicated>: Unary<Right, Predicate<Left, Predicated>>

Curried function that takes a Right and a Left value and returns a boolean representing a comparison between those values.

example
const biggerThan: Comparison<number> = right => left => right > left;

Type Parameters

  • Right

  • Left extends Right = Right

  • Predicated extends Left = Left

GenericFunction<Arguments, Output>: ((..._arguments: Arguments) => Output)

Type Parameters

Type declaration

    • (..._arguments: Arguments): Output
    • Generic function type (safer than using any).

      example
      const example = (callback: GenericFunction) => callback("test");
      

      Parameters

      • Rest ..._arguments: Arguments

      Returns Output

Predicate<Input, Predicated>: Unary<Input, boolean> | ((input: Input) => input is Predicated)

Function that returns boolean (Unary).

example
const isEven: Predicate<number> = n => n % 2 === 0;

Type Parameters

  • Input

  • Predicated extends Input = Input

Reducer<Item, Accumulator>: Unary<Item, Unary<Accumulator, Accumulator>>

Reducer/Folder function.

example
const array: ReadOnlyArray<number> = [1, 2, 3];
const reducer: Reducer<number, number> = item => accumulator => accumulator + item;

Type Parameters

  • Item

  • Accumulator

Sorter<Item>: Unary<Item, Unary<Item, number>>

Sorter function.

example
const array: ReadOnlyArray<number> = [1, 2, 3];
const sorter: Sorter<number> = value1 => value2 => value1 - value2;

Type Parameters

  • Item

Tagger<Output, Expressions>: ((templateStringsArray: Readonly<TemplateStringsArray>, ...expressions: Expressions) => Output)

Type Parameters

Type declaration

    • (templateStringsArray: Readonly<TemplateStringsArray>, ...expressions: Expressions): Output
    • Tag function for tagged templates.

      example
      const intParser: Tagger<number> = strings => parseInt(strings.join(""), 10);
      intParser`100`; // 100

      Parameters

      • templateStringsArray: Readonly<TemplateStringsArray>
      • Rest ...expressions: Expressions

      Returns Output

Unary<Input, Output>: ((input: Input) => Output)

Type Parameters

  • Input

  • Output

Type declaration

    • (input: Input): Output
    • Unary function (takes a single value).

      example
      const unary: Unary<number, number> = n => n + 1;
      

      Parameters

      • input: Input

      Returns Output

UnaryInput<UnaryFunction>: UnaryFunction extends Unary<infer Input, unknown> ? Input : never

Input type of an Unary.

example
const unary: Unary<number, string> = n => `${n}`;
UnaryInput<typeof unary> // `number`

Type Parameters

  • UnaryFunction extends Unary<never, unknown>

UnaryOutput<UnaryFunction>: UnaryFunction extends Unary<never, infer Output> ? Output : never

Output type of an Unary.

example
const unary: Unary<number, string> = n => `${n}`;
UnaryOutput<typeof unary> // `string`

Type Parameters

  • UnaryFunction extends Unary<never, unknown>

Number Type Aliases

Digit: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Valid digits (0 to 9).

example
const numbers: ReadOnlyArray<Digit> = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Object Type Aliases

Empty<ArrayRecordOrString>: ArrayRecordOrString extends ReadOnlyArray ? EmptyArray : ArrayRecordOrString extends ReadOnlyRecord ? EmptyRecord : ArrayRecordOrString extends string ? EmptyString : never

Empty array, object or string.

example
const emptyString: Empty<string> = "";
const emptyArray: Empty<ReadOnlyArray> = [];
const emptyRecord: Empty<ReadOnlyRecord> = {};

Type Parameters

EmptyRecord: ReadOnlyRecord<never>

Empty record (object).

example
const emptyRecord: EmptyRecord = {};
Entries<Key, Value>: ReadonlyArray<Entry<Key, Value>>

Array of Entry.

example
const entries: Entries<string, number> = [
["🟢", 1],
["🟩", 2],
];

Type Parameters

  • Key = PropertyKey

  • Value = unknown

EntriesOf<ArrayOrRecord>: Entries<KeyOf<ArrayOrRecord>, ValueOf<ArrayOrRecord>>

Array of EntryOf.

example
const object = {
"🟢": 1,
"🟩": 2,
};
const entries: EntriesOf<typeof object> = Object.entries(object);

Type Parameters

Entry<Key, Value>: readonly [key: Key, value: Value]

Entry couple [key, value].

example
const entry: Entry<string, number> = ["🟢", 1];

Type Parameters

  • Key = PropertyKey

  • Value = unknown

EntryKey<Input>: Input[0]

Key of an Entry.

example
const entry: Entry<string, number> = ["🟢", 1];
const entryKey: EntryKey<typeof entry> = entry[0];

Type Parameters

EntryOf<ArrayOrRecord>: Entry<KeyOf<ArrayOrRecord>, ValueOf<ArrayOrRecord>>

Object or array Entry.

example
const object = {
"🟢": 1,
"🟩": 2,
};
const entries: EntryOf<typeof object> = Object.entries(object)[0];

Type Parameters

EntryValue<Input>: Input[1]

Value of an Entry.

example
const entry: Entry<string, number> = ["🟢", 1];
const entryValue: EntryValue<typeof entry> = entry[1];

Type Parameters

KeyOf<ArrayOrRecord>: ArrayOrRecord extends ReadOnlyArray ? KeyOfArray<ArrayOrRecord> : keyof ArrayOrRecord extends Exclude<PropertyKey, symbol> ? `${keyof ArrayOrRecord}` : never

Generic key for either object or array.

Type Parameters

NotEmpty<Type>: Not<Empty<Type>, Type>

Not empty array, object or string.

example
const notEmptyString: NotEmpty<string> = "🟢";
const notEmptyArray: NotEmpty<ReadOnlyArray> = ["🟢", "🟩"];
const notEmptyRecord: NotEmpty<ReadOnlyRecord> = { "🟢": "🟩" };

Type Parameters

ReadOnlyRecord<Value, Key>: Readonly<Record<Key, Value>>

Read-only record.

example
const record: ReadOnlyRecord<number> = {
"🟢": 1,
"🟩": 2,
};

Type Parameters

  • Value = unknown

  • Key extends PropertyKey = PropertyKey

ValueOf<ArrayOrObject>: ArrayOrObject[KeyOf<ArrayOrObject>]

Generic key for either object or array.

example
const object: ReadOnlyRecord<number> = {
"🟢": 1,
"🟩": 2,
};
const key: ValueOf<typeof object> = 1;

Type Parameters

RegExp Type Aliases

RegularExpression: `/${string}/${RegularExpressionFlags}`

String representing a regular expression.

example
const regex: RegularExpression = "/^[a-z]+$/u";
RegularExpressionFlags: `${EmptyString | "g"}${EmptyString | "i"}${EmptyString | "m"}${EmptyString | "s"}u`

Possible combinations of regular expression flags (always unicode).

example
const flags1: RegularExpressionFlags = "u";
const flags2: RegularExpressionFlags = "gu";
const flags3: RegularExpressionFlags = "iu";
const flags4: RegularExpressionFlags = "giu";

String Type Aliases

Empty<ArrayRecordOrString>: ArrayRecordOrString extends ReadOnlyArray ? EmptyArray : ArrayRecordOrString extends ReadOnlyRecord ? EmptyRecord : ArrayRecordOrString extends string ? EmptyString : never

Empty array, object or string.

example
const emptyString: Empty<string> = "";
const emptyArray: Empty<ReadOnlyArray> = [];
const emptyRecord: Empty<ReadOnlyRecord> = {};

Type Parameters

EmptyString: ""

Empty string.

example
const emptyString: EmptyString = "";
Head<Input>: HeadAndTail<Input>[0]

Initial value of an array or string.

example
const array = ["🟢", "🟩", "💚"];
const head: Head<typeof array> = "🟢";

Type Parameters

  • Input extends ArrayLike<unknown>

HeadAndTail<Input>: Input extends readonly [infer Head, ...(infer Tail)] ? readonly [Head, Tail] : Input extends `${infer Head}${infer Tail}` ? readonly [Head, Tail] : Input extends EmptyArray | EmptyString ? readonly [undefined, Input] : readonly [Input[number] | undefined, Input]

Get a couple with the head and tail of an array or string.

example
const array = ["🟢", "🟩", "💚"];
const headAndTail: HeadAndTail<typeof array> = ["🟢", ["🟩", "💚"]];

Type Parameters

  • Input extends ArrayLike<unknown>

Initial<Input>: InitialAndLast<Input>[0]

Initial values of an array or string (omitting the last).

example
const array = ["🟢", "🟩", "💚"];
const initial: Initial<typeof array> = ["🟢", "🟩"];

Type Parameters

  • Input extends ArrayLike<unknown>

InitialAndLast<Input>: Input extends readonly [...(infer Initial), infer Last] ? readonly [Initial, Last] : Input extends EmptyArray | EmptyString ? readonly [Input, undefined] : Input extends `${infer Initial}${infer Rest}` ? readonly [`${Rest extends EmptyString ? EmptyString : Initial}${InitialAndLast<Rest>[0]}`, `${Rest extends EmptyString ? Initial : InitialAndLast<Rest>[1]}`] : readonly [Input, Input[number] | undefined]

Get a couple with the initial and last values of an array or string.

example
const array = ["🟢", "🟩", "💚"];
const initialAndLast: InitialAndLast<typeof array> = [["🟢", "🟩"], "💚"];

Type Parameters

  • Input extends ArrayLike<unknown>

Last<Input>: InitialAndLast<Input>[1]

Last value of an array or string.

example
const array = ["🟢", "🟩", "💚"];
const last: Last<typeof array> = "💚";

Type Parameters

  • Input extends ArrayLike<unknown>

NotEmpty<Type>: Not<Empty<Type>, Type>

Not empty array, object or string.

example
const notEmptyString: NotEmpty<string> = "🟢";
const notEmptyArray: NotEmpty<ReadOnlyArray> = ["🟢", "🟩"];
const notEmptyRecord: NotEmpty<ReadOnlyRecord> = { "🟢": "🟩" };

Type Parameters

Tagger<Output, Expressions>: ((templateStringsArray: Readonly<TemplateStringsArray>, ...expressions: Expressions) => Output)

Type Parameters

Type declaration

    • (templateStringsArray: Readonly<TemplateStringsArray>, ...expressions: Expressions): Output
    • Tag function for tagged templates.

      example
      const intParser: Tagger<number> = strings => parseInt(strings.join(""), 10);
      intParser`100`; // 100

      Parameters

      • templateStringsArray: Readonly<TemplateStringsArray>
      • Rest ...expressions: Expressions

      Returns Output

Tail<Input>: HeadAndTail<Input>[1]

Last values of an array or string (omitting the first).

example
const array = ["🟢", "🟩", "💚"];
const tail: Tail<typeof array> = ["🟩", "💚"];

Type Parameters

  • Input extends ArrayLike<unknown>