• Creates a proxy object that can intercept operations on a target object

    Type Parameters

    • T extends object

      Type of object to proxy (must be an object)

    Parameters

    • target: T

      Object to proxy

    • handler: ProxyHandler<T>

      Object containing proxy trap functions

    Returns T

    A proxy wrapping the target object

    const person = { firstName: "John", lastName: "Doe" };
    const withFullName = createProxy(person, {
    get: (obj, key) => {
    if (key === "fullName") {
    return `${obj.firstName} ${obj.lastName}`;
    }
    return obj[key];
    }
    });

    print(withFullName.fullName); // "John Doe"
    const numbers = createProxy([] as number[], {
    set: (obj, key, value) => {
    if (typeof value !== "number") {
    error("Only numbers allowed!");
    }
    obj[key] = value;
    }
    });

    numbers.push(42); // OK
    numbers.push("not a number"); // Throws error