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(图神经网络)和InCoder(支持填充中间任务的多语言通用Transformer)。我们的工具自动化了使用类型预测模型所需的几个步骤:(1)为项目依赖导入类型;(2)将JavaScript模块迁移到TypeScript表示法;(3)在需要时将预测的类型注释插入程序以生成TypeScript;(4)在需要时拒绝非类型预测。我们在一个包含513个JavaScript包的数据集上评估了TypeWeaver,其中包括从未被类型化的包。使用最佳类型预测模型,我们发现只有21%的包通过了类型检查,但更令人鼓舞的是,69%的文件成功通过了类型检查。