mockdeep / typewiz

Automatically discover and add missing types in your TypeScript code
https://medium.com/@urish/manual-typing-is-no-fun-introducing-typewiz-58e3e8813f4c
1.1k stars 46 forks source link

Enumerating object fields (object spread operator, etc.) #86

Open zxti opened 5 years ago

zxti commented 5 years ago

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!

urish commented 5 years ago

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 commented 5 years ago

@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?