Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/class/decorators.ts>throws

A JavaScript extension package for building strong and modern applications.
Latest
function throws
import { throws } from "https://deno.land/x/ayonli_jsext@v0.9.72/class/decorators.ts";

A decorator that restrains the thrown value of the method at runtime, based on Zod.

Examples

Example 1

import { throws } from "@ayonli/jsext/class/decorators";
import { z } from "zod";

class Calculator {
    \@throws(z.instanceof(RangeError))
    div(a: number, b: number) {
        if (b === 0) {
            throw new TypeError("division by zero");
        }
        return a / b;
    }
}

const calc = new Calculator();
console.log(calc.div(2, 0));
// throws:
// TypeError: validation failed at thrown value: not an instance of RangeError

Type Parameters

T extends z.ZodType