[Paper Review] OptTyper: Probabilistic Type Inference by Optimising Logical and Natural Constraints
OptTyper proposes a principled framework for probabilistic type inference in dynamic languages by unifying logical constraints (from static type analysis) and natural constraints (from deep learning on code patterns like naming conventions) into a single continuous optimization problem. By relaxing logical constraints into a differentiable form, it ensures predicted types satisfy type safety while leveraging learned patterns, achieving 76% Top-1 accuracy on TypeScript—4% better than prior work.
We present a new approach to the type inference problem for dynamic languages. Our goal is to combine \emph{logical} constraints, that is, deterministic information from a type system, with \emph{natural} constraints, that is, uncertain statistical information about types learnt from sources like identifier names. To this end, we introduce a framework for probabilistic type inference that combines logic and learning: logical constraints on the types are extracted from the program, and deep learning is applied to predict types from surface-level code properties that are statistically associated. The foremost insight of our method is to constrain the predictions from the learning procedure to respect the logical constraints, which we achieve by relaxing the logical inference problem of type prediction into a continuous optimisation problem. We build a tool called OptTyper to predict missing types for TypeScript files. OptTyper combines a continuous interpretation of logical constraints derived by classical static analysis of TypeScript code, with natural constraints obtained from a deep learning model, which learns naming conventions for types from a large codebase. By evaluating OptTyper, we show that the combination of logical and natural constraints yields a large improvement in performance over either kind of information individually and achieves a 4% improvement over the state-of-the-art.
Motivation & Objective
- To address the high annotation burden in dynamically typed languages like TypeScript by reducing reliance on manual type annotations.
- To improve probabilistic type inference by integrating deterministic logical constraints with uncertain statistical patterns from code.
- To ensure predicted types respect language-level type safety rules, which prior methods often violate.
- To unify logical and natural constraints into a single optimization framework that guarantees constraint satisfaction.
- To build a scalable, extensible system that can incorporate diverse learning models and existing type inference engines.
Proposed method
- Formalize type inference as a continuous optimization problem by relaxing hard logical constraints (from static analysis) into differentiable penalty terms.
- Use a deep learning model to predict type probabilities based on surface-level code features such as identifier names and lexical context.
- Combine the output of the deep learning model (natural constraints) with the relaxed logical constraints in a unified objective function.
- Optimize the joint objective using numerical solvers, ensuring all predictions satisfy the original type system constraints.
- Support arbitrary neural models for natural constraints and plug in any standard type inference engine for logical constraints.
- Integrate the framework into a tool called OptTyper, which predicts missing types in TypeScript files.
Experimental results
Research questions
- RQ1Can logical type constraints be effectively relaxed into a continuous optimization framework to guide probabilistic type predictions?
- RQ2Does combining logical and natural constraints in a single optimization step improve type inference accuracy compared to using either alone?
- RQ3Can the framework guarantee that predicted types respect the underlying type system rules, even when predictions are uncertain?
- RQ4How does the performance of the unified approach compare to state-of-the-art tools like LambdaNet and DeepTyper?
- RQ5To what extent do natural language patterns (e.g., naming conventions) contribute to improved type inference when constrained by logic?
Key findings
- OptTyper achieves 76% Top-1 accuracy on the standard TypeScript type inference benchmark, representing a 4% improvement over the previous state-of-the-art.
- The ablation study confirms that the performance gain is due to the principled integration of both logical and natural constraints, not just one source.
- The method guarantees that all predicted types satisfy the original type system constraints, unlike LambdaNet, which fails to do so in practice.
- The framework is extensible and can incorporate any deep learning model for natural constraints and any standard type inference engine for logical constraints.
- OptTyper outperforms TypeWriter in efficiency by avoiding a two-stage filtering process, as it enforces constraints during prediction.
- The continuous relaxation of logical constraints enables end-to-end differentiability and stable optimization, unlike discrete filtering approaches.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.