@illavv/run_typer - v3.0.2
    Preparing search index...

    Class Typer

    Class representing a type checker. Version: 2.4.1

    Michael Lavigna

    2.4.1

    Index

    Constructors

    Methods

    • Validates and returns an array

      Type Parameters

      • T = unknown

        The expected element type

      Parameters

      • value: unknown

        The value to validate

      Returns T[]

      The validated array

      If not an array

    • Validates and returns a boolean

      Parameters

      • value: unknown

        The value to validate

      Returns boolean

      The validated boolean

      If not a boolean

    • Validates and returns a number

      Parameters

      • value: unknown

        The value to validate

      Returns number

      The validated number

      If not a number

    • Validates and returns an object

      Type Parameters

      • T extends Record<string, unknown> = Record<string, unknown>

        The expected object type

      Parameters

      • value: unknown

        The value to validate

      Returns T

      The validated object

      If not an object

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

      Parameters

      • value: unknown

        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
    • Validates and returns a string

      Parameters

      • value: unknown

        The value to validate

      Returns string

      The validated string

      If not a string

    • Recursively validates an object against a nested schema.

      Parameters

      • schema: Record<string, unknown>

        The expected structure definition.

      • obj: Record<string, unknown>

        The object to validate.

      • path: string = ''

        The current path for error reporting (internal use).

      • strictMode: boolean = false

        Whether to reject extra keys not in schema.

      Returns StructureValidationReturn

      • Validation result with errors array.
      const schema = {
      name: "string",
      age: "number",
      hobbies: ["string"],
      address: {
      street: "string",
      city: "string?"
      }
      };
      const obj = { name: "John", age: 25, hobbies: ["reading"] };
      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.

        Defines the expected input and output types for a function.

        • paramTypes: string[]

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

        • returnType: string[]

          The expected return type(s) of the function

      Returns (...args: unknown[]) => 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.

      Type Parameters

      • T = unknown

        The expected type for better TypeScript inference

      Parameters

      • value: unknown

        The value to check.

      • types: string | string[]

        One or more types to check against.

      Returns value is T

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

      if (typer.is<string>("hello", "string")) {
      // value is now typed as string
      console.log(value.toUpperCase());
      }
      console.log(Typer.is(42, "number")); // true
      console.log(Typer.is("hello", ["string", "number"])); // true
      console.log(Typer.is([], "string")); // false
    • Type-safe array validation

      Type Parameters

      • T = unknown

        The expected element type

      Parameters

      • value: unknown

        The value to check

      Returns value is T[]

      Type guard for array

    • Checks if the provided parameter is an array of a specified type.

      Type Parameters

      • T = unknown

        The expected element type

      Parameters

      • elementType: string

        The type of elements that the array should contain.

      • p: unknown

        The parameter to check.

      Returns T[]

      Typed array of elements

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

      const numbers = typer.isArrayOf<number>("number", [1, 2, 3]); // numbers: number[]
      const strings = typer.isArrayOf<string>("string", ["a", "b"]); // strings: string[]
    • Type-safe boolean validation

      Parameters

      • value: unknown

        The value to check

      Returns value is boolean

      Type guard for boolean

    • Checks if the provided parameter is a valid email address.

      Parameters

      • p: unknown

        The parameter to check.

      Returns string

      The validated email string

      Throws if the parameter is not a valid email address.

      const email = typer.isEmail("test@example.com"); // email: string
      
    • Checks if the provided parameter is a number within a specified range.

      Parameters

      • min: number

        The minimum value.

      • max: number

        The maximum value.

      • p: unknown

        The parameter to check.

      Returns number

      The validated number

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

      const age = typer.isInRange(18, 65, 25); // age: number
      
    • Checks if the provided parameter is an integer.

      Parameters

      • p: unknown

        The parameter to check.

      Returns number

      The validated integer

      Throws if the parameter is not an integer.

      const count = typer.isInteger(42); // count: number
      
    • Checks if the provided parameter is a negative integer.

      Parameters

      • p: unknown

        The parameter to check.

      Returns number

      The validated negative integer

      Throws if the parameter is not a negative integer.

      const count = typer.isNegativeInteger(-42); // count: number
      
    • Checks if the provided parameter is a negative number.

      Parameters

      • p: unknown

        The parameter to check.

      Returns number

      The validated negative number

      Throws if the parameter is not a negative number.

      const value = typer.isNegativeNumber(-10); // value: number
      
    • Checks if the provided parameter is a non-empty array.

      Type Parameters

      • T = unknown

        The expected element type

      Parameters

      • p: unknown

        The parameter to check.

      Returns T[]

      The validated non-empty array

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

      const items = typer.isNonEmptyArray<string>(["a", "b"]); // items: string[]
      
    • Checks if the provided parameter is a non-empty string.

      Parameters

      • p: unknown

        The parameter to check.

      Returns string

      The validated non-empty string

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

      const name = typer.isNonEmptyString("Hello"); // name: string
      
    • Type-safe number validation

      Parameters

      • value: unknown

        The value to check

      Returns value is number

      Type guard for number

    • Type-safe object validation

      Type Parameters

      • T extends Record<string, unknown> = Record<string, unknown>

        The expected object type

      Parameters

      • value: unknown

        The value to check

      Returns value is T

      Type guard for object

    • Checks if the provided parameter is one of the specified values.

      Type Parameters

      • T

        The expected type of the values

      Parameters

      • values: readonly T[]

        The values to check against.

      • p: unknown

        The parameter to check.

      Returns T

      The validated value

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

      const color = typer.isOneOf(["red", "blue", "green"] as const, "blue"); // color: "red" | "blue" | "green"
      
    • Checks if the provided parameter is a valid phone number.

      Parameters

      • p: unknown

        The parameter to check.

      Returns string

      The validated phone number string

      Throws if the parameter is not a valid phone number.

      const phone = typer.isPhoneNumber("+1234567890"); // phone: string
      const phone2 = typer.isPhoneNumber("(555) 123-4567"); // phone2: string
    • Checks if the provided parameter is a positive integer.

      Parameters

      • p: unknown

        The parameter to check.

      Returns number

      The validated positive integer

      Throws if the parameter is not a positive integer.

      const count = typer.isPositiveInteger(42); // count: number
      
    • Checks if the provided parameter is a positive number.

      Parameters

      • p: unknown

        The parameter to check.

      Returns number

      The validated positive number

      Throws if the parameter is not a positive number.

      const value = typer.isPositiveNumber(10); // value: number
      
    • Type-safe string validation

      Parameters

      • value: unknown

        The value to check

      Returns value is string

      Type guard for string

    • Check if the parameter matches one of the specified types.

      Type Parameters

      • T = unknown

        The expected type for better TypeScript inference

      Parameters

      • types: string | string[]

        The types to check against.

      • p: unknown

        The parameter to check.

      Returns T

      Returns the value cast to the expected type

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

      const value = typer.isType<string>("string", "Hello"); // value is typed as string
      console.log(Typer.isType("string", "Hello")); // "Hello"
      console.log(Typer.isType(["number", "boolean"], 42)); // 42
      console.log(Typer.isType(["number", "boolean"], "text")); // Throws TypeError
    • Checks if the provided parameter is a valid URL.

      Parameters

      • p: unknown

        The parameter to check.

      Returns string

      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.

      Type Parameters

      • T = unknown

        The input type that the validator expects

      • R = T

        The return type that the validator produces

      Parameters

      • name: string

        The name of the new type.

      • validator: (value: T) => R

        The function to validate the type.

      • override: boolean = false

        Whether to override the original configuration

      Returns void

      If the type name is already registered.

      // Register a positive number validator
      typer.registerType<unknown, number>("positive", (value) => {
      if (typeof value !== "number" || value <= 0) throw new TypeError("Must be positive");
      return value;
      });

      // Register a string length validator
      typer.registerType<unknown, string>("longString", (value) => {
      if (typeof value !== "string" || value.length < 10) throw new TypeError("Must be long string");
      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, unknown>

        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"]