facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.09k stars 1.86k forks source link

Flow do not find map property in Proxy<Array<any>> #3435

Open pizzafroide opened 7 years ago

pizzafroide commented 7 years ago
// @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
jesseschalken commented 7 years ago

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?

pizzafroide commented 7 years ago

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();
jesseschalken commented 7 years ago

Oh, that I didn't know about. Sounds like a bug, then.