typewiz icon indicating copy to clipboard operation
typewiz copied to clipboard

Enumerating object fields (object spread operator, etc.)

Open zxti opened this issue 7 years ago • 2 comments

Typewiz seems to change objects by replacing their fields with property getters and setters—this makes sense, since it needs to track the types of the values being written.

However, wherever one uses something like Object.assign(), the spread operator, lodash.extend, etc., things seem to break—these functions aren't able to enumerate the fields (since they aren't real fields).

Do you have any suggestions on dealing with this? Thanks!

zxti avatar Dec 04 '18 05:12 zxti

I spent some time thinking about this, and I think using Proxy objects (along with some decorators) could do the trick. I prototyped something quick here, just to check feasibility:

https://stackblitz.com/edit/typewiz-proxy-experiment?file=index.ts

(check out the console output when running this example)

It seems like this would do the trick for the most part, except for tracking properties that are assigned value during the construction of the object. Can you please check if this approach would work better with the functions you mentioned?

urish avatar Dec 04 '18 19:12 urish

@MadaraUchiha @benjamingr perhaps you have some idea how to do it better?

Currently, typewiz replaces properties with setter/getters, so we can instrument the setters. I was thinking about using Proxy object, so this is more transparent to the user. However, it seems like this will miss everything that happens inside the constructor, unless there is some creative way to proxy the this within the constructor too?

urish avatar Dec 05 '18 22:12 urish