Class representing a type checker. Version: 2.4.1

Michael Lavigna

2.4.1

Constructors

Methods

  • Assert that a value is of a specific type. Logs a warning if incorrect.

    Parameters

    • value: any

      The value to check.

    • expectedType: string | string[]

      The expected type(s).

    Returns void

    Typer.assert(42, "number"); // No output
    Typer.assert("hello", "number"); // Warning in console
  • Recursively validates an object against a nested schema.

    Parameters

    • schema: Record<string, any>

      The expected structure.

    • obj: Record<string, any>

      The object to validate.

    • path: string = ''

    Returns StructureValidationReturn

    • List of errors, or an empty array if valid.
    const schema = {
    name: "string",
    age: "number",
    };
    const obj = { name: "John", age: 25 };
    console.log(Typer.checkStructure(schema, obj)); // { isValid: true, errors: [] }
  • Expects a function to conform to specified input and output types.

    Parameters

    • funct: Function

      The function to type-check.

    • types: TyperExpectTypes

      The expected types for the function's parameters and return value.

      • paramTypes: string[]

        The expected type(s) of the function's parameters

      • returnType: string[]

        The expected return type(s) of the function

    Returns (...args: any) => any

    A new function that type-checks its arguments and return value.

    If the types object does not contain exactly 3 keys or the required type properties.

    If the function or types object does not conform to the expected types.

    const typedFunction = Typer.expect(
    (x: number) => x * 2,
    { paramTypes: ["number"], returnType: ["number"] }
    );
    console.log(typedFunction(3)); // 6
  • Exports all registered types as a JSON string.

    Returns string

    The serialized types.

    console.log(Typer.exportTypes()); // '["array","number","string","boolean"]'
    
  • Imports types from a JSON string.

    Parameters

    • json: string

      The JSON string containing type names.

    Returns void

    Typer.importTypes('["customType"]');
    
  • Checks if the provided value matches one or more specified types.

    Parameters

    • value: any

      The value to check.

    • types: string | string[]

      One or more types to check against.

    Returns boolean

    Returns true if the value matches any type, false otherwise.

    console.log(Typer.is(42, "number")); // true
    console.log(Typer.is("hello", ["string", "number"])); // true
    console.log(Typer.is([], "string")); // false
  • Checks if the provided parameter is an array of a specified type.

    Parameters

    • elementType: string

      The type of elements that the array should contain.

    • p: any

      The parameter to check.

    Returns TyperReturn<any[]>

    Throws if the parameter is not an array of the specified type.

    console.log(Typer.isArrayOf("number", [1, 2, 3])); // true
    console.log(Typer.isArrayOf("string", [1, 2, "hello"])); // false
  • Checks if the provided parameter is a valid email address.

    Parameters

    • p: any

      The parameter to check.

    Returns TyperReturn<string>

    Throws if the parameter is not a valid email address.

    console.log(Typer.isEmail("test@example.com")); // true
    console.log(Typer.isEmail("invalid-email")); // false
  • Checks if the provided parameter is a number within a specified range.

    Parameters

    • min: number

      The minimum value.

    • max: number

      The maximum value.

    • p: any

      The parameter to check.

    Returns TyperReturn<number>

    Throws if the parameter is not a number within the specified range.

    console.log(Typer.isInRange(1, 10, 5)); // true
    console.log(Typer.isInRange(1, 10, 20)); // false
  • Checks if the provided parameter is an integer.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not an integer.

    console.log(Typer.isInteger(42)); // true
    console.log(Typer.isInteger(42.5)); // false
  • Checks if the provided parameter is a positive integer.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not a positive integer.

    console.log(Typer.isNegativeInteger(-42)); // true
    console.log(Typer.isNegativeInteger(10)); // Throws TypeError
  • Checks if the provided parameter is a positive number.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not a positive number.

    console.log(Typer.isNegativeNumber(-10)); // true
    console.log(Typer.isNegativeNumber(5)); // Throws TypeError
  • Checks if the provided parameter is a non-empty array.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not a non-empty array.

    console.log(Typer.isNonEmptyArray([1, 2, 3])); // true
    console.log(Typer.isNonEmptyArray([])); // Throws TypeError
  • Checks if the provided parameter is a non-empty string.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not a non-empty string.

    console.log(Typer.isNonEmptyString("Hello")); // true
    console.log(Typer.isNonEmptyString("")); // Throws TypeError
  • Checks if the provided parameter is one of the specified values.

    Parameters

    • values: any[]

      The values to check against.

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not one of the specified values.

    console.log(Typer.isOneOf(["red", "blue"], "blue")); // true
    console.log(Typer.isOneOf(["red", "blue"], "green")); // false
  • Checks if the provided parameter is a valid phone number.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not a valid phone number.

    console.log(Typer.isPhoneNumber("+1234567890")); // true
    console.log(Typer.isPhoneNumber("abc123")); // Throws TypeError
  • Checks if the provided parameter is a positive integer.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not a positive integer.

    console.log(Typer.isPositiveInteger(42)); // true
    console.log(Typer.isPositiveInteger(-10)); // Throws TypeError
  • Checks if the provided parameter is a positive number.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not a positive number.

    console.log(Typer.isPositiveNumber(10)); // true
    console.log(Typer.isPositiveNumber(-5)); // Throws TypeError
  • Check if the parameter matches one of the specified types.

    Parameters

    • types: string | string[]

      The types to check against.

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter does not match any of the specified types.

    console.log(Typer.isType("string", "Hello")); // true
    console.log(Typer.isType(["number", "boolean"], 42)); // true
    console.log(Typer.isType(["number", "boolean"], "text")); // Throws TypeError
  • Checks if the provided parameter is a valid URL.

    Parameters

    • p: any

      The parameter to check.

    Returns any

    Throws if the parameter is not a valid URL.

    console.log(Typer.isURL("https://example.com")); // true
    console.log(Typer.isURL("invalid-url")); // false
  • Get all registered types.

    Returns string[]

    An array of registered type names.

    console.log(Typer.listTypes()); // ["array", "number", "string", "boolean"]
    
  • Register a new type in the typesMap.

    Parameters

    • name: string

      The name of the new type.

    • validator: (value: any) => any

      The function to validate the type.

    • override: boolean = false

      Whether to override the original configuration

    Returns void

    If the type name is already registered.

    Typer.registerType("positive", (value) => {
    if (typeof value !== "number" || value <= 0) throw new TypeError("Must be positive");
    return value;
    });
  • Unregister a type from the typesMap.

    Parameters

    • name: string

      The name of the type to remove.

    Returns void

    If the type does not exist.

    Typer.unregisterType("positive");
    
  • Validates an object against a schema.

    Parameters

    • schema: Record<string, string | string[]>

      The expected types for each key.

    • obj: Record<string, any>

      The object to validate.

    Returns string[]

    • An array of validation errors, or an empty array if valid.
    const schema = { name: "string", age: "number" };
    const obj = { name: "John", age: "25" };
    console.log(Typer.validate(schema, obj)); // ["Expected 'age' to be of type number, got string"]