Abstract
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.
Supplemental Material
- M. Abadi, L. Cardelli, B. Pierce, and G. Plotkin. Dynamic typing in a statically-typed language. In Proceedings of POPL, 1989. Google Scholar
Digital Library
- A. Ahmed, R. B. Findler, J. G. Siek, and P. Wadler. Blame for all. In Proceedings of POPL, 2011. Google Scholar
Digital Library
- E. Allende, J. Fabry, and É. Tanter. Cast insertion strategies for gradually-typed objects. In Proceedings of DLS, 2013. Google Scholar
Digital Library
- E. Allende, J. Fabry, R. Garcia, and É. Tanter. Confined gradual typing. In Proceedings of OOPSLA, 2014. Google Scholar
Digital Library
- C. Anderson, P. Giannini, and S. Drossopoulou. Towards type inference for JavaScript. In Proceedings of ECOOP, 2005. Google Scholar
Digital Library
- G. Bierman, M. Abadi, and M. Torgersen. Understanding TypeScript. In Proceedings of ECOOP, 2014.Google Scholar
- R. Cartwright and M. Fagan. Soft typing. In Proceedings of PLDI, 1991. Google Scholar
Digital Library
- R. Chugh, D. Herman, and R. Jhala. Dependent types for JavaScript. In Proceedings of OOSLA, 2012. Google Scholar
Digital Library
- M. Fagan. Soft Typing: An Approach to Type Checking for Dynamically Typed Languages. PhD thesis, 1990. Google Scholar
Digital Library
- A. Feldthaus and A. Müller. Checking correctness of TypeScript interfaces for JavaScript libraries. In Proceedings of OOPSLA, 2014. Google Scholar
Digital Library
- A. Guha, C. Saftoiu, and S. Krishnamurthi. The essence of JavaScript. In Proceedings of ECOOP, 2010. Google Scholar
Digital Library
- A. Guha, C. Saftoiu, and S. Krisnamurthi. Typing local control and state using flow analysis. In Proceedings of ESOP, 2011. Google Scholar
Digital Library
- F. Henglein and J. Rehof. Safe polymorphic type inference for Scheme: Translating Scheme to ML. In Proceedings of FPCA, 1995. Google Scholar
Digital Library
- D. Herman, A. Tomb, and C. Flanagan. Space-efficient gradual typing. Higher Order Symbol. Comput., 2010. Google Scholar
Digital Library
- B. S. Lerner, J. G. Politz, A. Guha, and S. Krishnamurthi. TeJaS: Retrofitting type systems for JavaScript. In Proceedings of DLS, 2013. Google Scholar
Digital Library
- Z. Luo. Coercive subtyping. J. Log. Comput., 9 (1): 105--130, 1999.Google Scholar
Cross Ref
- M. S. Miller, M. Samuel, B. Laurie, I. Awad, and M. Stay. Caja: Safe active content in sanitized JavaScript. Unpublished paper, 2007.Google Scholar
- A. Rastogi, N. Swamy, C. Fournet, G. Bierman, and P. Vekris. Safe & efficient gradual typing for TypeScript, MSR-TR-2014--99, 2014.Google Scholar
- G. Richards, F. Z. Nardelli, C. Rouleau, and J. Vitek. Types you can count on. Unpublished paper, 2014.Google Scholar
- J. G. Siek and W. Taha. Gradual typing for functional languages. In Proceedings of Scheme and functional programming workshop, 2006.Google Scholar
- J. G. Siek and W. Taha. Gradual typing for objects. In Proceedings of ECOOP, 2007. Google Scholar
Digital Library
- J. G. Siek and M. M. Vitousek. Monotonic references for gradual typing. Unpublished paper, 2013.Google Scholar
- N. Swamy, J. Weinberger, C. Schlesinger, J. Chen, and B. Livshits. Verifying higher-order programs with the Dijkstra monad. In Proceedings of PLDI, 2013. Google Scholar
Digital Library
- N. Swamy, C. Fournet, A. Rastogi, K. Bhargavan, J. Chen, P.-Y. Strub, and G. Bierman. Gradual typing embedded securely in JavaScript. In Proceedings of POPL, 2014. Google Scholar
Digital Library
- A. Takikawa, T. S. Stickland, C. Dimoulas, S. Tobin-Hochstadt, and M. Felleisen. Gradual typing for first-class classes. In Proceedings of OOPSLA, 2012. Google Scholar
Digital Library
- P. Thiemann. Towards a type systems for analyzing JavaScript programs. In Proceedings of ESOP, 2005. Google Scholar
Digital Library
- N. Tillmann, M. Moskal, J. de Halleux, M. Fähndrich, and S. Burckhardt. Touchdevelop: app development on mobile devices. In Proceedings of FSE, 2012. Google Scholar
Digital Library
- S. Tobin-Hochstadt and M. Felleisen. Interlanguage migration: from scripts to programs. In Proceedings of DLS, 2006. Google Scholar
Digital Library
- S. Tobin-Hochstadt and M. Felleisen. The design and implementation of Typed Scheme. In Proceedings of POPL, 2008. Google Scholar
Digital Library
- J. Turner. Announcing TypeScript 1.1 CTP, 2014. http://blogs.msdn.com/b/typescript/archive/2014/10/06/announcing-typescript-1--1-ctp.aspx.Google Scholar
- M. M. Vitousek, A. M. Kent, J. G. Siek, and J. Baker. Design and evaluation of gradual typing for Python. In Proceedings of DLS, 2014. Google Scholar
Digital Library
- P. Wadler and R. B. Findler. Well-typed programs can't be blamed. In Proceedings of ESOP, 2009. Google Scholar
Digital Library
- A. K. Wright and R. Cartwright. A practical soft type system for scheme. ACM Trans. Program. Lang. Syst., 19 (1): 87--152, Jan. 1997. 10.1145/239912.239917. Google Scholar
Digital Library
- lund, and Vitek}ThornT. Wrigstad, F. Z. Nardelli, S. Lebresne, J. Östlund, and J. Vitek. Integrating typed and untyped code in a scripting language. In Proceedings of POPL, 2010. Google Scholar
Digital Library
Index Terms
Safe & Efficient Gradual Typing for TypeScript
Recommendations
Safe & Efficient Gradual Typing for TypeScript
POPL '15: Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming LanguagesCurrent 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 ...
Refinement types for TypeScript
PLDI '16: Proceedings of the 37th ACM SIGPLAN Conference on Programming Language Design and ImplementationWe present Refined TypeScript (RSC), a lightweight refinement type system for TypeScript, that enables static verification of higher-order, imperative programs. We develop a formal system for RSC that delineates the interaction between refinement types ...
Fast and precise type checking for JavaScript
In this paper we present the design and implementation of Flow, a fast and precise type checker for JavaScript that is used by thousands of developers on millions of lines of code at Facebook every day. Flow uses sophisticated type inference to ...







Comments