type Article: "a" | "an"

No description.

type Type: "array" | "NaN" | "null" | "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"

Extended typeof. Used in RuntimeTypeCheck.getType.

List of possible conditions, any of which may match. Thus, it is a disjunction of conjunctions (OR list of AND lists).

Either a single condition that must match, or a list of conditions, all of which must match.

class Cond

Cond(): Cond

No description.

static typeof: (type: string) => Condition

Assert a value to be of Type.

Assert a value to be a boolean.

Assert a value to be a function.

Assert a value to be a number.

Assert a value to be a string.

static true: Condition

Assert a value to be true. Implies boolean.

static false: Condition

Assert a value to be false. Implies boolean.

Assert a value to be an integer (only whole numbers). Implies number.

static array: (...descriptor: Descriptor) => Condition & Condition

Generate a condition that asserts a value to be an array, optionally with the given descriptor inside it.

This function itself is a condition without inner types, so it can be used as Cond.array as an alias to Cond.array().

static object: (keyName?: string, ...descriptor: Descriptor) => Condition & Condition

Generate a condition that asserts a value to be an object literal, optionally with the given descriptor inside it.

This function itself is a condition without inner types, so it can be used as Cond.object as an alias to Cond.object().

Param (keyName)

A concise key description used when displaying the type: Object<keyName, ...>.

Assert a value to be positive. Implies number.

Assert a value to be a non-empty string or a non-empty array. Implies string OR array.

static keywords(...keywords: string[]): Condition

Generate a condition that asserts a value to be only the specified strings. Implies string.

static length(len: number): Condition

Generate a condition that asserts a value to be of the given length. Implies string OR array.

class TypeCheckError extends Error

TypeCheckError(expected: string, is: string): TypeCheckError

No description.

expected: string

No description.

is: string

No description.

class RuntimeTypeCheck

No description.

static Cond: Cond

No description.

static assertAndThrow(
val: any,
...descriptor: Descriptor
): boolean

Assert an arbitrary value to match any of the given conditions and throw a detailed explanatory error message if the assertion fails.

The conditions are tested recursively through their potential Condition.conditions field.

Parameters

  • val: The value to test.

  • descriptor: The conditions to test the value against.

static assert(val: any, ...descriptor: Descriptor): boolean

Assert an arbitrary value to match any of of the given conditions.

The conditions are tested recursively through their potential Condition.conditions field.

Parameters

  • val: The value to test.

  • descriptor: The conditions to test the value against.

static assertFind(
val: any,
...descriptor: Descriptor
): undefined | Condition

If a given arbitrary value does not assert any of the given conditions, return the most relevant failing condition in the context of the given value.

The conditions are tested recursively through their potential Condition.conditions field.

Parameters

  • val: The value to test.

  • descriptor: The conditions to test the value against.

Remarks

This method probably doesn't have to concern you. It is used within getMessageIs to get the most relevant condition for the resulting message.

Example

assertFind(3.2, Cond.string, [ Cond.positive, Cond.integer ], Cond.true);

Returns Cond.integer because it is the most relevant failing condition for the value 3.2.

static getMostRelevantFailingCondition(
val: any,
...descriptor: Descriptor
): undefined | Condition

Recursively count passing conditions, weighted by shallowness, and return the failing condition in the context of the maximum pass count.

In other words, find the failing condition whose sibling and children conditions pass the most amount of assertions for the given value and are thus the most relevant condition context.

This does not check if a descriptor as a whole asserts to true.

See also assertFind

Internal

static getMessageIsIterated(
val: any[],
...descriptor: Descriptor
): undefined | string

Return the result of getMessageIs for the first of the passed values that does not assert.

Parameters

  • val: An array of the values to test.

  • descriptor: The conditions to test the value against.

static getMessageIs(
val: any,
...descriptor: Descriptor
): string

Return a string denoting the type of the value in the context of the condition that is closest to matching, or an empty string if all conditions assert correctly.

The returned string is simply the result of executing the Condition.is field of the failing condition.

Parameters

  • val: The value to test.

  • descriptor: The conditions to test the value against. The first failed condition will produce the return value.

Example

Value:        `0`
Conditions:   `[ Cond.number, Cond.positive ]`
Return value: `"a negative number or 0"`
static getMessageExpected(...descriptor: Descriptor): string

Return a string denoting the expected type within the passed conditions. The message is created by recursively merging all shouldBe fields.

Example

descriptor 1: `[ Cond.number, Cond.positive ]`
descriptor 2: `Cond.keywords("foobar")`
Return value: `'positive number OR the keyword "foobar"'`
static compilePartialMessage(
messagePartial: MessagePartial
): string

Compile a single MessagePartial into a coherent sentence of the form "[...before] [type] [...after]". A few linguistic transformations are made.

Example

Input:

{
  before: [ 'nonverbal', 'positive' ],
  type: [ 'integer' ],
  after: [ 'that is cool', 'with 6 digits', 'that is divisible by 5' ]
}

Output: "nonverbal, positive integer with 6 digits that is cool and is divisible by 5"

Internal

static mergeDescriptorMessages(
...descriptor: Descriptor
): MessagePartial[]

Recursively look at the messages defined in the shouldBe field of the given descriptors and merge them from bottom to top, returning a disjunction of MessagePartials.

Disjunction means that every output list item is a standalone message for a seperate assertion (OR).

Internal

static getArticle(value: string): "a" | "an"

Get the matching indefinite article (a or an) for the passed string.

Remarks

This method is by no means linguistically sound, it simply checks whether the first character of the passed string is a vowel.

static getPrettyEnumeratedList(list: string[]): string

Get a string list of all items of the passed string array of the style "first, second, third or fourth".

Remarks

This is used within the RuntimeTypeCheck.Cond.keywords condition.

static getType(value: any): Type

Return the typeof of a value with the additional types 'array', 'NaN' and 'null'.

interface IsData

Data describing attributes of a value that did not pass a condition's assertion. Utilized in Condition.is.

val: any

No description.

No description.

No description.

interface ExpectedData

type: string

No description.

interface Message

Contains the three parts of the message used to denote the expected type of the form "[before] [type] [after]".

Message parts from extended conditions (via Condition.conditions) will be inherited and merged.

Example

Extending RuntimeTypeCheck.Cond.number will inherit its type as "number". Building a before: "positive" on top of it will create the following Message:

{
  before: 'positive',
  type: 'number',
}

And yield the following sentence: "Expected positive number [...]".

before?: string

No description.

type?: string

No description.

after?: string

No description.

interface Condition

Further conditions that this condition relies on. The passed value in assert is ensured to match these conditions.

Note that this is a Descriptor, so it is an OR list of AND lists.

For example, a condition asserting a positive number may specify the condition of type number.

assert: (value: any) => boolean

Assertion function for an arbitrary value.

The passed value is ensured to match the specified Descriptor in Condition.conditions, if any.

Denote what the expected value should be.

See also Message.

Example

Assuming the condition asserts a positive number and specifies the further condition RuntimeTypeCheck.Cond.number. The attribute "positive" can then simply be added to the front of the type. The type itself ("number") is contributed by the extended condition, so we don't have to care about that:

shouldBe: { before: 'positive' }

The final error message will then be: "Expected positive number, got [...]."

is: string | (data: IsData) => string

Sentence denoting what the value is when it does not assert. In simple cases, this is usually a description of the opposite of the assertion. The sentence should always start with a lowercase indefinite article (a or an).

For advanced use cases, a function may be specified which takes an IsData object that contains useful information about the value in question.

Example

Simple
Assuming the condition asserts a positive number, the `is`
value could be "a negative number or 0", which is a
description of the exact opposite of the expected value.

The final error message will then be: "Expected [...], got a negative number or 0."

Example (Advanced)

Assuming the condition asserts the length of a value and the specified further conditions are therefore RuntimeTypeCheck.Cond.array and RuntimeTypeCheck.Cond.string.

The type or value of the value are unknown, so we can utilize the method to get type and article and append a sentence describing the value:

({ type, article }) => `${article} ${type} of a different length`.

In case of an array, the final error message will then be: "Expected [...], got an array of a different length"