Creates a proxy object that can intercept operations on a target object
Type of object to proxy (must be an object)
Object to proxy
Object containing proxy trap functions
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" Copy
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); // OKnumbers.push("not a number"); // Throws error Copy
const numbers = createProxy([] as number[], { set: (obj, key, value) => { if (typeof value !== "number") { error("Only numbers allowed!"); } obj[key] = value; }});numbers.push(42); // OKnumbers.push("not a number"); // Throws error
Creates a proxy object that can intercept operations on a target object