Alternative Tools: dts-gen and Others
What you'll learn: A survey of tools that can accelerate JavaScript-to-TypeScript migration by generating declaration files and inferring types from runtime behavior.
Beyond Manual Declaration Files
When migrating JavaScript projects to TypeScript, you don't always have to write declaration files or type annotations from scratch. Several community tools can automate parts of this process, complementing approaches like ts-migrate and JSDoc.
dts-gen: Declaration File Generator
dts-gen is a tool that generates .d.ts declaration files by analyzing JavaScript modules at runtime. It executes your JavaScript code and observes the actual types of exports, functions, and objects.
The basic idea: you install a JavaScript package, run dts-gen against it, and it produces a declaration file based on what it discovers:
# Install dts-gen globally
npm install -g dts-gen
# Generate declarations for an installed package
dts-gen -m some-untyped-library
This creates a starting-point .d.ts file in your current directory. You'll still need to review and refine it, but it beats starting from a blank page.
Limitations: dts-gen can only see what gets executed. If a function accepts multiple types conditionally, or if some code paths aren't triggered, the generated types may be incomplete or overly specific.
TypeWiz: Runtime Type Inference
TypeWiz takes a different approach: it instruments your JavaScript code during development, observes actual values passed to functions as you run your application or tests, and then suggests type annotations based on observed usage.
This is particularly useful when you have good test coverage, because TypeWiz learns from real execution patterns. It adds instrumentation, collects type data, and outputs suggested annotations you can apply.
Trade-off: Requires actually running code paths. Unused functions won't get type suggestions, and observed types might not cover all possible cases.
Other Migration Helpers
- TypeStat: Mentioned earlier, focuses on adding type annotations based on usage analysis
- TypeScript Language Service: Your editor's built-in "Add all missing imports" and quick-fix actions can auto-generate basic type stubs
- Community Scripts: Many teams build custom AST-based tools using the TypeScript Compiler API to handle project-specific patterns
When These Tools Help
These tools shine when:
- You have large, untyped JavaScript codebases with no documentation
- Third-party dependencies lack
@typespackages - You want a starting point rather than perfection
- Your team can follow up with manual refinement
They're not magic bullets—expect to spend significant time cleaning up generated types, adding constraints, and handling edge cases the tools missed.
Key Takeaway: Tools like dts-gen (runtime-based declaration generation) and TypeWiz (runtime inference) can jumpstart migration by producing initial types automatically, but always require human review and refinement to achieve type safety.