Type migration is the process of adding types to untyped code to gain assurance at compile time. TypeScript and other gradual type systems facilitate type migration by allowing programmers to start with imprecise types and gradually strengthen them. However, adding types is a manual effort and several migrations on large, industry codebases have been reported to have taken several years. In the research community, there has been significant interest in using machine learning to automate TypeScript type migration. Existing machine learning models report a high degree of accuracy in predicting individual TypeScript type annotations. However, in this paper we argue that accuracy can be misleading, and we should address a different question: can an automatic type migration tool produce code that passes the TypeScript type checker? We present TypeWeaver, a TypeScript type migration tool that can be used with an arbitrary type prediction model. We evaluate TypeWeaver with three models from the literature: DeepTyper, a recurrent neural network; LambdaNet, a graph neural network; and InCoder, a general-purpose, multi-language transformer that supports fill-in-the-middle tasks. Our tool automates several steps that are necessary for using a type prediction model, (1) importing types for a project's dependencies; (2) migrating JavaScript modules to TypeScript notation; (3) inserting predicted type annotations into the program to produce TypeScript when needed; and (4) rejecting non-type predictions when needed. We evaluate TypeWeaver on a dataset of 513 JavaScript packages, including packages that have never been typed before. With the best type prediction model, we find that only 21% of packages type check, but more encouragingly, 69% of files type check successfully.
翻译:类型迁移是指为无类型代码添加类型标注,从而在编译时获得保障的过程。TypeScript及其他渐进类型系统允许程序员从粗糙的类型开始逐步强化类型,从而促进类型迁移。然而,添加类型标注是手动操作,且据报告,大型工业代码库的多次迁移耗时数年之久。研究界对利用机器学习自动化TypeScript类型迁移产生了浓厚兴趣。现有机器学习模型在预测单个TypeScript类型标注时报告了较高的准确率。然而,本文认为准确率可能具有误导性,应聚焦于一个不同的问题:自动化类型迁移工具能否生成通过TypeScript类型检查器的代码?我们提出TypeWeaver,这是一个可配合任意类型预测模型使用的TypeScript类型迁移工具。我们使用文献中的三种模型评估TypeWeaver:递归神经网络DeepTyper、图神经网络LambdaNet,以及支持填充中间任务的多语言通用Transformer模型InCoder。我们的工具自动化了使用类型预测模型所需的多个步骤:(1)导入项目依赖的类型;(2)将JavaScript模块迁移至TypeScript语法;(3)在必要时将预测的类型标注插入程序以生成TypeScript代码;(4)在必要时拒绝非类型预测。我们在包含513个JavaScript包的数据集上评估TypeWeaver,其中包括从未进行类型标注的包。使用最佳类型预测模型时,仅21%的包通过类型检查;但更令人鼓舞的是,69%的文件成功通过类型检查。