// @flow
let a = new Proxy([], {});
a.map(i => i);
This gives the following errors:
3: g.map(i => i);
^^^ property `map`. Property not found in
3: g.map(i => i);
^ Proxy
I'm not sure if there's a way to define Proxy in lib/core.js so that it is considered to implement the type that it wraps. That may need special support in the type checker. As a workaround you could define a wrapper like this:
function mkproxy<T>(x: T, handlers: Proxy$traps<T>): Proxy<T> & T {
return new Proxy(x, handlers);
}
let a = new mkproxy([], {});
a.map(i => i);
Strictly speaking, I don't see how Proxy could be type safe since the whole point of it is to be able to override practically any language-level operation (property get/set, call, new etc etc) on the wrapped object. How can Flow know whether the resulting Proxy is a suitable substitute for the object it wraps?
I agree that Proxy cannot be type safe. But what I don't understand is that there is an error when the target is an Array while there are none when the target is of another type. For instance, the following code passes without any errors:
// @flow
let a = new Proxy({ bar(){} }, {});
a.bar();
Oh, that I didn't know about. Sounds like a bug, then.
Most helpful comment
I agree that
Proxycannot be type safe. But what I don't understand is that there is an error when the target is anArraywhile there are none when the target is of another type. For instance, the following code passes without any errors: