Class TypeGuardError<T>

Custom error class thrown when runtime assertion fails in typia.assert<T>() function.

This error is thrown by the typia.assert<T>() function when the input value doesn't match the expected type.

The error provides detailed information about the first assertion failure encountered, including the access path where the error occurred, the expected type, and the actual value.

Jeongho Nam - https://github.com/samchon

interface IMember {
name: string;
age: number & ExclusiveMinimum<19>;
}

try {
typia.assert<IMember>({ name: "John", age: 18 });
} catch (error) {
if (error instanceof TypeGuardError) {
console.log(error.method); // "typia.assert"
console.log(error.path); // "input.age"
console.log(error.expected); // "number & ExclusiveMinimum<19>"
console.log(error.value); // 18
}
}

Type Parameters

  • T = any

    The expected type (generic for type safety)

Hierarchy

  • Error
    • TypeGuardError

Constructors

Properties

expected: string

String representation of the expected type at the error location.

Represents TypeScript types as strings, including detailed type information for complex types.

- `"string"` - Expected string type
- `"number & ExclusiveMinimum<19>"` - Expected number greater than 19
- `"undefined"` - Expected undefined (when superfluous property found in assertion)
- `"{ name: string; age: number }"` - Expected object type
message: string
method: string

The name of the typia method that threw this error.

"typia.assert"
name: string
path: undefined | string

The access path to the property where the assertion error occurred.

Uses dot notation to indicate the path for nested object properties. May be undefined if the error occurred at the root level.

- `"input.age"` - Error in the age property of the object
- `"input.profile.email"` - Error in the email property of a nested object
- `"input[0].name"` - Error in the name property of the first array element
- `undefined` - Error occurred at the root level
stack?: string
value: any

The actual value that failed assertion.

Stores the actual value at the error path as-is. Useful for debugging by comparing the expected type with the actual value.

- `18` - Numeric value
- `"invalid"` - String value
- `{ name: "John", age: 18, sex: 1 }` - Object value
stackTraceLimit: number

The Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)).

The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed.

If set to a non-number value, or set to a negative number, stack traces will not capture any frames.

Methods

  • Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

    const myObject = {};
    Error.captureStackTrace(myObject);
    myObject.stack; // Similar to `new Error().stack`

    The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

    The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

    The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

    function a() {
    b();
    }

    function b() {
    c();
    }

    function c() {
    // Create an error without stack trace to avoid calculating the stack trace twice.
    const { stackTraceLimit } = Error;
    Error.stackTraceLimit = 0;
    const error = new Error();
    Error.stackTraceLimit = stackTraceLimit;

    // Capture the stack trace above function b
    Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
    throw error;
    }

    a();

    Parameters

    • targetObject: object
    • OptionalconstructorOpt: Function

    Returns void

  • Parameters

    • err: Error
    • stackTraces: CallSite[]

    Returns any