Options
All
  • Public
  • Public/Protected
  • All
Menu

@mehra/ts

Index

Type aliases

AlphaChar

AlphaChar: A_Z_Upper | A_Z_Lower

A character matching /^[a-zA-Z]$/

AlphaNumChar

AlphaNumChar: AlphaChar | NumChar

A character matching /^[a-zA-Z\d]$/

Async

Async<T>: T | Promise<T> | PromiseLike<T>

Types that unwrap (via await or .then) to {@typeparam T}

example

An interface where functions are allowed to be asynchronous (Promise-returning), but do not need to be

Type parameters

  • T = void

DFAAccepts

DFAAccepts<DfaT, InputT>: AcceptsImpl<DfaT, DfaT["startState"], InputT>

Does {@typeparam DfaT} accept {@typeparam InputT}?

Type true, false, or never. Will broaden to boolean if you pass a union type where at least one gives true and one gives false.

Type parameters

  • DfaT: DFA

  • InputT: string

Digit

Digit: NumChar

Arabic numerals (base 10 digits)

Email

Email: string & { [emailDisc]: unique symbol }

A nominal string type for emails

InvertedMatrix

InvertedMatrix<BaseSet, ValueSet>: {[ index in BaseSet]: PartialRecord<ValueSet, BaseSet[]> }

Encodes multiple (not necessarily symmetric) relations of members of {@typeparam BaseSet}

For each member of {@typeparam BaseSet}, allows to write in which members of {@typeparam ValueSet} apply between it and another member, then enumerates each satisfying pair.

Use object-relation syntax to extract members:

invMatrix[X]["rel1"]
example

A symmetric example

const invMatrix: InvertedMatrix<0 | 1 | 2, "neighbors" | "friends"> = {
  0: {
    neighbors: [2],
  },
  1: {
    friends: [2],
    neighbors: [0, 2],
  },
  2: {
    friends: [1],
    neighbors: [0, 1],
  },
};
example

An asymmetric example

const invMatrix: InvertedMatrix<0 | 1 | 2, "strongerThan" | "heavierThan"> = {
 0: {
   strongerThan: [2],
   heavierThan: [1, 2],
 },
 1: {
   strongerThan: [0, 2],
   heavierThan: [2],
 },
 2: {},
};
example

A silly example

const invMatrix: InvertedMatrix<0 | 1 | 2, "normal" | "special"> = {
  0: {
    normal: [2],
    special: [1, 0],
  },
  1: {
    normal: [0, 1],
    special: [2],
  },
  2: {
    normal: [0, 1, 2],
  },
};

This is silly because it seems there are only two options, normal and special. If that's true, just record pairs where it is special:

const invMatrix: InvertedMatrix<0 | 1 | 2, "special"> = {
  0: {
    special: [0, 1],
  },
  1: {
    special: [2],
  },
  2: {},
};

This is still not optimal, though, since it seems you know that only one relation exists for any ordered pair. In that case, a Matrix or PartialMatrix should be used instead.

Type parameters

  • BaseSet: keyof any

    The set to describe relations of

  • ValueSet: keyof any

    Contains the names of the relations

IsAlpha

IsAlpha<T>: DFAAccepts<AlphaDFA, T>

Does {@typeparam T} match /^[a-zA-Z]*$/? That is, is it a string composed of only English letters?

Type true, false, or never. Will broaden to boolean if you pass a union type where at least one gives true and one gives false.

Type parameters

  • T: string

IsAlphaNum

IsAlphaNum<T>: DFAAccepts<AlphaNumDFA, T>

Does {@typeparam T} match /^[a-zA-Z\d]*$/? That is, is it a string composed of only English letters and Arabic numerals?

Type true, false, or never. Will broaden to boolean if you pass a union type where at least one gives true and one gives false.

Type parameters

  • T: string

IsDirectInt

IsDirectInt<T>: DFAAccepts<DirectIntDFA, T>

Does {@typeparam T} match /^\d+$/? That is, is it a non-empty string composed of only Arabic numerals?

Type true, false, or never. Will broaden to boolean if you pass a union type where at least one gives true and one gives false.

Type parameters

  • T: string

IsEmail

IsEmail<T>: DFAAccepts<EmailDFA, T>

Type parameters

  • T: string

IsEqual

IsEqual<A, B>: IsSubType<true, IsSubType<A, B> & IsSubType<B, A>>

Type true if {@typeparam A} :< {@typeparam B} && {@typeparam B} :< {@typeparam A}

see

IsSubType for a unidirectional variant.

see

RequireEqual for a stricter variant.

Type parameters

  • A

  • B

IsIntDecimal

IsIntDecimal<T>: DFAAccepts<SignedIntDecimalDFA, T>

Does {@typeparam T} match /^[+-]?\d+([eE][+-]?\d+)?$/? That is, is it a common decimal or decimal-scientific form of an (possibly signed) integer?

Type true, false, or never. Will broaden to boolean if you pass a union type where at least one gives true and one gives false.

Type parameters

  • T: string

IsNonEmptyAlpha

IsNonEmptyAlpha<T>: DFAAccepts<NonEmptyAlphaDFA, T>

Does {@typeparam T} match /^[a-zA-Z]+$/? That is, is it a non-empty string composed of only English letters?

Type true, false, or never. Will broaden to boolean if you pass a union type where at least one gives true and one gives false.

Type parameters

  • T: string

IsNonEmptyAlphaNum

IsNonEmptyAlphaNum<T>: DFAAccepts<NonEmptyAlphaNumDFA, T>

Does {@typeparam T} match /^[a-zA-Z\d]+$/? That is, is it a non-empty string composed of only English letters and Arabic numerals?

Type true, false, or never. Will broaden to boolean if you pass a union type where at least one gives true and one gives false.

Type parameters

  • T: string

IsSubType

IsSubType<A, B>: A extends B ? true : false

Type true if {@typeparam A} is a subtype of {@typeparam B}; otherwise type false.

see

IsEqual for a bidirectional variant.

see

RequireSubType for a stricter variant.

Type parameters

  • A

  • B

IsUnsignedIntDecimal

IsUnsignedIntDecimal<T>: DFAAccepts<UnsignedIntDecimalDFA, T>

Does {@typeparam T} match /^\d+([eE][+-]?\d+)?$/? That is, is it a common decimal or decimal-scientific form of an unsigned integer?

Type true, false, or never. Will broaden to boolean if you pass a union type where at least one gives true and one gives false.

Type parameters

  • T: string

Matrix

Matrix<BaseSet, ValueSet>: Record<BaseSet, Record<BaseSet, ValueSet>>

A function {@typeparam BaseSet}^2 --> {@TypeParam ValueSet}

Assigns a member of {@typeparam ValueSet} to each member of {@typeparam BaseSet}^2

Use double array syntax to extract members:

matrix[X][Y]
example
const matrix: Matrix<0 | 1 | 2, "normal" | "special"> = {
  0: {
    0: "special",
    1: "special",
    2: "normal",
  },
  1: {
    0: "normal",
    1: "normal",
    2: "special",
  },
  2: {
    0: "normal",
    1: "normal",
    2: "normal",
  },
};

However, if there is a reasonable default member of ValueSet, a PartialMatrix may be a better solution.

Type parameters

  • BaseSet: keyof any

  • ValueSet

NonEmptyArray

NonEmptyArray<T, U>: [T, ...U[]]

Arrays with at least one element

Type parameters

  • T

    The type of the first element

  • U = T

    The type of the remaining elements, if different from {@typeparam T}

NumChar

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

Arabic numerals (base 10 digits)

PartialMatrix

PartialMatrix<BaseSet, ValueSet>: PartialRecord<BaseSet, PartialRecord<BaseSet, ValueSet>>

A partial function {@typeparam BaseSet}^2 ~~> {@TypeParam ValueSet}

Optionally assigns a member of {@typeparam ValueSet} to each member of {@typeparam BaseSet}^2.

Use optional double array syntax to extract members:

matrix[X]?.[Y]
example
const partialMatrix: PartialMatrix<0 | 1 | 2, "special"> = {
  0: {
    0: "special",
    1: "special",
  },
  1: {
    2: "special",
  },
};

Contrast this with the implementation using the non-partial Matrix. However, in this case, you could also likely write

const specials = {
  0: [0, 1],
  1: [2],
};

This type is far more useful when you have more than 2 values for your function, in this case normal, more, and less:

const partialMatrix: PartialMatrix<0 | 1 | 2, "more" | "less"> = {
  0: {
    0: "more",
    1: "less",
  },
  1: {
    2: "less",
  },
  2: {
    2: "more",
  },
};

Type parameters

  • BaseSet: keyof any

  • ValueSet

PartialRecord

PartialRecord<A, B>: Partial<Record<A, B>>

Type parameters

  • A: keyof any

  • B

Predicate

Predicate<T>: (value: T) => boolean

Type parameters

  • T

Type declaration

    • (value: T): boolean
    • Parameters

      • value: T

      Returns boolean

RequireEqual

RequireEqual<A, B, C>: IsEqual<A, B>

Requires {@typeparam C} :< IsSubType<A, B>; otherwise issues a compile-time error. Has value equal to IsSubType<A, B>.

Type parameters

RequireSubType

RequireSubType<A, B>: IsSubType<A, B>

A stricter version of IsSubType. Type true if {@typeparam A} is a subtype of {@typeparam B}; compile-time error otherwise.

Type parameters

  • A: B

  • B

SubType

SubType<A, B>: TestedType<A, IsSubType<A, B>>

Type {@typeparam A} if {@typeparam A} is a subtype of {@typeparam B}; otherwise type never.

Type parameters

  • A

  • B

TestedType

TestedType<T, Bool>: Bool extends true ? T : never

{@typeparam T} if {@typeparam Bool}, else never

Type parameters

  • T

  • Bool

Functions

Assert

  • Assert(bool: boolean, errorConstructor?: new () => Error): asserts bool
  • Asserts {@param bool}

    throws

    {@param errorConstructor}

    Parameters

    • bool: boolean
    • errorConstructor: new () => Error = ...

      A constructor for the thrown error class

        • new (): Error
        • Returns Error

    Returns asserts bool