一句话:提出 DeepTyper,用深度学习结合编译器为 JavaScript 等动态语言预测并补充类型标注,准确率高。
针对 JavaScript/Python 等动态语言静态类型化成本高、传统类型推断难以可靠处理鸭子类型与 eval 的问题,本文提出 DeepTyper:一个基于深度学习的类型推断模型。它从自动对齐的 token 与类型语料中学习类型在特定上下文和关系中的自然分布,为变量和函数预测类型标注。作者发现上下文对准确赋值至关重要,并提出缓解对局部线索过拟合的技术。评估表明,DeepTyper 能预测数千个类型标注,并可同编译器交互,额外提供 4,000 多个初始无法推断的类型标注,精确率超过 95%。
Dynamically typed languages such as JavaScript and Python are increasingly popular, yet static typing has not been totally eclipsed: Python now supports type annotations and languages like TypeScript offer a middle-ground for JavaScript: a strict superset of JavaScript, to which it transpiles, coupled with a type system that permits partially typed programs. However, static typing has a cost: adding annotations, reading the added syntax, and wrestling with the type system to fix type errors. Type inference can ease the transition to more statically typed code and unlock the benefits of richer compile-time information, but is limited in languages like JavaScript as it cannot soundly handle duck-typing or runtime evaluation via eval. We propose DeepTyper, a deep learning model that understands which types naturally occur in certain contexts and relations and can provide type suggestions, which can often be verified by the type checker, even if it could not infer the type initially. DeepTyper, leverages an automatically aligned corpus of tokens and types to accurately predict thousands of variable and function type annotations. Furthermore, we demonstrate that context is key in accurately assigning these types and introduce a technique to reduce overfitting on local cues while highlighting the need for further improvements. Finally, we show that our model can interact with a compiler to provide more than 4,000 additional type annotations with over 95% precision that could not be inferred without the aid of DeepTyper.