Computing Publications

Publications Home » Type Inference for JavaScript

Type Inference for JavaScript

Christopher Anderson

PhD Thesis
Department of Computing, Imperial College London
March, 2006

Object-oriented scripting languages like JavaScript are popular, in part because of their dynamic features. These include the runtime modification of objects and classes, through addition of fields or updating of methods. These features make static typing difficult and usually dynamic typing is used. Consequently, errors such as access to non-existent members, are not detected until runtime.

We provide a static type system that can cope with dynamic features such as member addition, while providing the usual safety guarantees. Since the structure of objects may change over time, we employ a structural type system to track the changes. We show how type inference can be used to infer the structure of objects and give corresponding structural types. Therefore, the programmer can enjoy the safety offered by static typing, without having to give explicit types in their programs.

We develop JS, a formalisation of Javascript with features including dynamic addition of fields and updating of methods. We give an operational semantics and static type system for JS0 using structural types. Our types allow objects to evolve in a controlled manner by classifying members as definite or potential. A member is potential until it has been assigned to and then it becomes definite. We prove that our type system is sound. We develop a type inference algorithm for JS0 based on a system of constraints between type variables. We define a translation between constraints and types that allows us to generate a type annotated JS0 program from an untyped JS0 program. We prove that the constraints are sound with respect to the type system and that our translation is deterministic. We define a well-formedness criterion on constraints and conjecture that well-formed constraints are satisfiable. Therefore, combined with the soundness of constraints and the soundness of the type system, we conjecture that programs that generate well-formed constraints will not get stuck.

PDF of full publication (1.9 megabytes)
(need help viewing PDF files?)
BibTEX file for the publication
Conditions for downloading publications from this site. built & maintained by Ashok Argent-Katwala.