机器学习模型能否生成可通过类型检查的 TypeScript 类型?
Do Machine Learning Models Produce TypeScript Types That Type Check?
AI总结:
本文研究机器学习能否生成通过 TypeScript 类型检查的代码,提出可与任意类型预测模型配合的迁移工具 TypeWeaver,并在 513 个 JavaScript 包上评估,发现仅 21% 的包通过类型检查,但 69% 的文件通过。
摘要:
类型迁移是向无类型代码添加类型以在编译期获得保证的过程。TypeScript 及其他渐进类型系统通过允许程序员从不够精确的类型开始并逐步加强类型,来促进类型迁移。然而,添加类型是一项人工工作,并且已有报道称在大型工业代码库上的若干迁移耗时数年。在研究界,使用机器学习来自动化 TypeScript 类型迁移引起了显著关注。现有机器学习模型在预测单个 TypeScript 类型注解方面报告了很高的准确率。然而,在本文中我们认为准确率可能具有误导性,我们应当解决一个不同的问题:自动类型迁移工具能否生成通过 TypeScript 类型检查器的代码?我们提出 TypeWeaver,一个可与任意类型预测模型配合使用的 TypeScript 类型迁移工具。我们使用文献中的三个模型评估 TypeWeaver:DeepTyper,一种循环神经网络;LambdaNet,一种图神经网络;以及 InCoder,一种支持中间填充任务的通用多语言 Transformer。我们的工具自动化了使用类型预测模型所必需的若干步骤:(1) 为项目依赖导入类型;(2) 将 JavaScript 模块迁移为 TypeScript 表示法;(3) 在需要时插入预测的类型注解以生成 TypeScript;(4) 在需要时拒绝非类型预测。我们在一个包含 513 个 JavaScript 包的数据集上评估 TypeWeaver,其中包括从未被类型化的包。使用最佳类型预测模型,我们发现只有 21% 的包通过类型检查,但更令人鼓舞的是,69% 的文件成功通过类型检查。
英文摘要:
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.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。