-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instrument JS code to detect types at runtime #7742
Comments
duplicate of #7546? |
This is not a duplicate. This is actually a pretty cool idea that I was considering as a possibility a while back also. Effectively you have unannotated code, then you run it (either launch under the debugger, run the tests, whatever), and it has been instrumented (by our emitter) to take note of all the function param types that were observed at run-time. You could then have a simple UI gesture to go and add the types observed at runtime to the code annotations, rather than having to figure them out and type them in yourself. (This is also a potentially cool feature for if you already have type annotations, we could flag or assert at runtime if a different type is observed for debugging). |
@billti cool. So how would you take on that? My idea is to transpile js/ts to this instrumented form using |
I think would really help with TypeScript adoption. I recently wanted to generate some Package managers could even potentially hook into the process and generate these files where appropriate. |
Noticed that https://github.com/Microsoft/dts-gen hadn't been mentioned yet, although it probably isn't as advanced as is being talked about here. |
@andy-ms thanks, I didn't know that existed! Perhaps @RyanCavanaugh needs to promote it more instead of spending so much time on typescript 😃 |
#26588 covers basic functionality. Leaving this issue open to cover instrumenting JS code for even better types. |
Assembled a quick demo of MetaES running small example: http://metaes.org/playground.html. Pushing original idea a bit forward:
I don't know if this should be a part of TypeScript server though, maybe a command line tool or IDE plugin. Or optional TS extension under a flag. That's something to discuss. |
The idea is to instrument JavaScript code and collect types information from the runtime. For example:
Consider JavaScript code (in ES2015):
File
render.js
:Then we use it in file
main.js
:Running this code as-is gives no help, but if it's instrumented then we may callect runtime information.
main.js
could be instrumented to the following form using esprima/escodegen or TypeScript's parser API:CallExpression
body could roughly look like:CollectArgumentTypesInformation
forcontact
parameter would yield something like:that could be later transformed into interface
contact
(name of the interface is based onrenderContact
1st parameter name):Later on this TypeScript type could be injected into the file like
render.ts
(next to therender.js
) and then be used to generated.ts
file using TypeScript'stsc --declaration
. Before runningtsc --declaration
author could refactor names for better naming and the instrumentation and types collection tool would respect that changes and wouldn't override them.position
would be used to source-map.js
and.ts
files. Or it could be something similar, maybe usingsourceMaps
project itself.The text was updated successfully, but these errors were encountered: