Do Machine Learning Models Produce TypeScript Types That Type Check?

Ming-Ho Yee, Arjun Guha

Abstract

The migration of large-scale JavaScript codebases to TypeScript is a resource-intensive process. While machine learning models have been proposed to automate type annotation, they are traditionally evaluated on their ability to match human-provided labels (top-k accuracy). This paper introduces TypeWeaver, a framework that evaluates models based on a more critical metric: whether the resulting code actually passes the TypeScript compiler (tsc). Our evaluation of three distinct architectures—RNNs, GNNs, and LLMs—reveals that while file-level success is promising (up to 69%), full package-level migration remains a significant challenge, with even the best models succeeding only 21% of the time.

Introduction

As TypeScript becomes the industry standard for web development, many organizations face the daunting task of migrating legacy JavaScript codebases. This process is often manual, requiring developers to navigate complex dependency graphs and internal logic to assign correct types. To alleviate this burden, researchers have developed various Machine Learning (ML) models to predict type annotations.

However, a significant gap exists between research metrics and practical utility. Most existing literature evaluates models using "top-k accuracy," which measures how often a model's prediction matches a ground-truth label provided by a human. This metric is flawed for two reasons: it ignores the strict requirements of the TypeScript compiler, and it fails to penalize "safe" but unhelpful predictions like any. This study addresses these shortcomings by introducing TypeWeaver, a tool designed to validate the real-world applicability of ML-generated types.

The TypeWeaver Pipeline

TypeWeaver is an end-to-end pipeline that transforms untyped JavaScript into validated TypeScript. Unlike previous approaches that focus solely on prediction, TypeWeaver handles the complex infrastructure required for a project to compile:

  1. Dependency Management: It ensures that all third-party project dependencies have corresponding type definitions (.d.ts files) available, allowing the compiler to resolve external symbols.
  2. Module Conversion: It refactors CommonJS modules (using require) into ECMAScript Modules (ESM), a prerequisite for modern TypeScript cross-file type tracking.
  3. Type Weaving: It integrates raw model predictions back into the original source code's Abstract Syntax Tree (AST).
  4. Validation: It executes the TypeScript compiler (tsc) to verify the semantic correctness of the annotated code.

Experimental Methodology

The authors evaluated three representative model architectures:

  • DeepTyper: A recurrent neural network (RNN) that treats type prediction as a sequence-to-sequence translation task.
  • LambdaNet: A graph neural network (GNN) that represents code as a dependency graph, modeling relationships between variables and functions.
  • InCoder: A large language model (LLM) from Meta designed for "fill-in-the-middle" code completion tasks.

The evaluation used a dataset of 513 JavaScript packages sourced from the top 1,000 npm downloads. This included a "DefinitelyTyped" subset for ground-truth comparison and a "Never Typed" subset to test performance on real-world, legacy code.

Results and Analysis

The Illusion of Accuracy

The study found that high accuracy does not necessarily correlate with compilable code. For instance, LambdaNet often predicted complex, human-like types that were technically accurate in spirit but failed the compiler's strict checks due to minor discrepancies. Conversely, DeepTyper achieved a high "success" rate by defaulting to any or any[] for approximately 60% of its predictions. While this satisfies the compiler, it provides zero type safety, rendering the migration effectively useless.

The LLM Advantage

InCoder, the LLM, emerged as the most effective model. Its ability to understand context through its "fill-in-the-middle" training allowed it to produce the most "type-correct" code. At the file level, InCoder achieved a success rate of 69%. However, the package-level success rate dropped significantly to 21%. This discrepancy highlights the difficulty of maintaining type consistency across an entire project's dependency graph.

Common Failure Modes

The most frequent error encountered was TS2339 ("Property does not exist on type"). This usually occurs when models fail to account for dynamic JavaScript patterns, such as adding properties to an object after its initialization. Because TypeScript requires these properties to be defined in the type interface, simple annotation is often insufficient.

Discussion: Infrastructure as a Bottleneck

A key finding of the research is that many failures were not due to the models themselves, but rather the underlying infrastructure. Converting from CommonJS to ESM and resolving complex, nested dependencies proved to be significant hurdles. Furthermore, many JavaScript idioms are "untypable" without structural refactoring. Current models only add labels; they do not rewrite the underlying logic to be more "TypeScript-friendly."

Conclusion

While fully automated, "one-click" migration from JavaScript to TypeScript is not yet a reality, ML models—particularly LLMs—show great promise in assisting the process. TypeWeaver demonstrates that we must move beyond simple accuracy metrics toward compiler-aware evaluation. Future work should focus on "human-in-the-loop" systems and models capable of performing structural refactoring alongside type annotation to handle the dynamic nature of legacy JavaScript.

Summary updated

BibTeX
@inproceedings{yee2023do,
  author = {Ming-Ho Yee and Arjun Guha},
  title = {{Do Machine Learning Models Produce TypeScript Types That Type Check?}},
  booktitle = {ECOOP},
  year = {2023},
  doi = {10.4230/lipics.ecoop.2023.37},
}
Figure