Ecma262: Why must ProxyHandler’s `ownKeys(…)` method’s return value contain only strings or symbols?

Created on 10 Dec 2019  Â·  2Comments  Â·  Source: tc39/ecma262

A lot of JavaScript user code assumes that you can pass arbitrary values where JavaScript expects property keys.

Examples:

  • Object.defineProperty(…)
  • Object.getOwnPropertyDescriptor(…)
  • Reflect.defineProperty(…)
  • Reflect.deleteProperty(…)
  • Reflect.get(…)
  • Reflect.getOwnPropertyDescriptor(…)
  • Reflect.has(…)
  • Reflect.set(…)

This makes [[ProxyHandler]].ownKeys(…) into a footgun, as user code might assume that the contents of the returned array‑like will be processed by ToPropertyKey(…), instead of throwing when it encounters a value that is neither a string, nor a symbol, like it does currently.


See also: https://github.com/microsoft/TypeScript/pull/35594

question

Most helpful comment

Without historical context on how the decision was made, it could be seen as an instance of robustness principle (Postel's law): the functions that you listed (consumers of property keys) are liberal in what they accept, but we require the proxy hooks to be conservative in what they produce.

All 2 comments

Without historical context on how the decision was made, it could be seen as an instance of robustness principle (Postel's law): the functions that you listed (consumers of property keys) are liberal in what they accept, but we require the proxy hooks to be conservative in what they produce.

cc @erights @tvcutsem

Was this page helpful?
0 / 5 - 0 ratings