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

x/ayonli_jsext/class/decorators.ts>param

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

A decorator that restrains the input arguments of the method at runtime, based on Zod.

NOTE: Although this decorator accepts a parameter name, it is not bound to the actually parameter in the method definition, it is only used for the error message. The parameter is bound by the appearance order of the decorator.

Examples

Example 1

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

class Calculator {
    \@param("a", z.number())
    \@param("b", z.number())
    add(a: number, b: number) {
        return a + b;
    }
}

const calc = new Calculator();
// \@ts-ignore for demonstration
console.log(calc.add("2", 3));
// throws:
// TypeError: validation failed at parameter a: expected number, received string

Type Parameters

T extends z.ZodType

Parameters

name: string
type: T