Safe & Efficient Gradual Typing for TypeScript

Current proposals for adding gradual typing to JavaScript, such as Closure, TypeScript and Dart, forgo soundness to deal with issues of scale, code reuse, and popular programming patterns. We show how to address these issues in practice while retaining soundness. We design and implement a new gradual type system, prototyped for expediency as a 'Safe' compilation mode for TypeScript. Our compiler achieves soundness by enforcing stricter static checks and embedding residual runtime checks in compiled code. It emits plain JavaScript that runs on stock virtual machines. Our main theorem is a simulation that ensures that the checks introduced by Safe TypeScript (1) catch any dynamic type error, and (2) do not alter the semantics of type-safe TypeScript code. Safe TypeScript is carefully designed to minimize the performance overhead of runtime checks. At its core, we rely on two new ideas: differential subtyping, a new form of coercive subtyping that computes the minimum amount of runtime type information that must be added to each object; and an erasure modality, which we use to safely and selectively erase type information. This allows us to scale our design to full-fledged TypeScript, including arrays, maps, classes, inheritance, overloading, and generic types. We validate the usability and performance of Safe TypeScript empirically by type-checking and compiling around 120,000 lines of existing TypeScript source code. Although runtime checks can be expensive, the end-to-end overhead is small for code bases that already have type annotations. For instance, we bootstrap the Safe TypeScript compiler (90,000 lines including the base TypeScript compiler): we measure a 15% runtime overhead for type safety, and also uncover programming errors as type safety violations. We conclude that, at least during development and testing, subjecting JavaScript/TypeScript programs to safe gradual typing adds significant value to source type annotations at a modest cost.

[1]  Anders Møller,et al.  Checking correctness of TypeScript interfaces for JavaScript libraries , 2014, OOPSLA.

[2]  Jan Vitek,et al.  Integrating typed and untyped code in a scripting language , 2010, POPL '10.

[3]  FournetCédric,et al.  Safe & Efficient Gradual Typing for TypeScript , 2015 .

[4]  Juan Chen,et al.  Gradual typing embedded securely in JavaScript , 2014, POPL.

[5]  Sam Tobin-Hochstadt,et al.  Gradual typing for first-class classes , 2012, OOPSLA '12.

[6]  Sophia Drossopoulou,et al.  Towards Type Inference for JavaScript , 2005, ECOOP.

[7]  Sebastian Burckhardt,et al.  TouchDevelop: app development on mobile devices , 2012, SIGSOFT FSE.

[8]  Fritz Henglein,et al.  Safe polymorphic type inference for a dynamically typed language: translating Scheme to ML , 1995, FPCA '95.

[9]  Martín Abadi,et al.  Understanding TypeScript , 2014, ECOOP.

[10]  Ravi Chugh,et al.  Dependent types for JavaScript , 2012, OOPSLA '12.

[11]  Zhaohui Luo,et al.  Coercive Subtyping , 1995 .

[12]  Joe Gibbs Politz,et al.  TeJaS: retrofitting type systems for JavaScript , 2013, DLS '13.

[13]  Walid Taha,et al.  Gradual Typing for Objects , 2007, ECOOP.

[14]  Cormac Flanagan,et al.  Space-efficient gradual typing , 2010, High. Order Symb. Comput..

[15]  Peter Thiemann Towards a Type System for Analyzing JavaScript Programs , 2005, ESOP.

[16]  Sam Tobin-Hochstadt,et al.  The design and implementation of typed scheme , 2008, POPL '08.

[17]  Éric Tanter,et al.  Confined gradual typing , 2014, OOPSLA.

[18]  Shriram Krishnamurthi,et al.  The Essence of JavaScript , 2010, ECOOP.

[19]  Shriram Krishnamurthi,et al.  Typing Local Control and State Using Flow Analysis , 2011, ESOP.

[20]  Martín Abadi,et al.  Dynamic typing in a statically-typed language , 1989, POPL '89.

[21]  Éric Tanter,et al.  Cast insertion strategies for gradually-typed objects , 2013, DLS '13.

[22]  Jeremy G. Siek,et al.  Monotonic References for Gradual Typing , 2013, ArXiv.

[23]  Philip Wadler,et al.  Well-Typed Programs Can't Be Blamed , 2009, ESOP.

[24]  Jeremy G. Siek Gradual Typing for Functional Languages , 2006 .

[25]  Benjamin Livshits,et al.  Verifying higher-order programs with the dijkstra monad , 2013, PLDI.

[26]  Fritz Henglein,et al.  Safe Polymorphic Type Inference for Scheme: Translating Scheme to ML. , 1995 .

[27]  Robert Cartwright,et al.  Soft typing , 1991, PLDI '91.

[28]  Philip Wadler,et al.  Blame for all , 2011, POPL '11.

[29]  Mike Fagan,et al.  Soft typing: an approach to type checking for dynamically typed languages , 1992 .

[30]  Jim Baker,et al.  Design and evaluation of gradual typing for python , 2014, DLS.

[31]  R. Charach The Appendix , 1948, The Lancet.

[32]  Sam Tobin-Hochstadt,et al.  Interlanguage migration: from scripts to programs , 2006, OOPSLA '06.