3 ms·
Is there any automated tool that can add types? I have Playwright E2E tests – it seems reasonable that the type each variable is used as could be recorded and t
by BrianHenryIE 3y ago
Is there any automated tool that can add types? I have Playwright E2E tests – it seems reasonable that the type each variable is used as could be recorded and the code annotated with that.
- chriskrycho 3y agoThe specific thing you’re asking for there is possible but non-trivial if you try to do it at runtime with tools like Playwright; more importantly it’s more overhead than you really need, and runtime types are often not exactly the same as what you want to describe an API, as you’ll be looking at various objects where you then have to decide how much of the prototype chain to walk and lots of other decisions like that. The way most tools in the space work is by leaning on TS’ own programmatic API. The two most interesting such tools I know of are AirBnB’s ts-migrate and LinkedIn’s RehearsalJS (full disclosure: I was loosely involved in design discussions for the matter, but did not contribute to it code-wise).
- idontwantthis 3y agoI wouldn’t recommend that. You want your types to have meaning for the business and you want to derive types from each other. It’s counterproductive to individually type every variable without context.
- rocqua 3y agoIn python at least, with strict-mode, Any is useless because you can do almost nothing to it. You can print, you can call 'dir' 'help' and other built-ins, and you can pass it to functions that accept Any. That means that sprinkling any around your code isn't a great way to silence errors.
- idontwantthis 3y agoI don’t know about Python, but for converting JS to TS you start with allow-implicit-any turned on and add types to your business objects as you go. Eventually your turn it off and fix what remains.
- chriskrycho 3y agoNote that while a lot of people do it this way, my blog post this discussion is notionally about on expressly argues that that’s exactly what not to do!
- idontwantthis 3y agoI have now, in fact, RTFA! You definitely make a lot of good points.