skip to main content
research-article

Safe & Efficient Gradual Typing for TypeScript

Published:14 January 2015Publication History
Skip Abstract Section

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.

Skip Supplemental Material Section

Supplemental Material

2676971.mov

References

  1. M. Abadi, L. Cardelli, B. Pierce, and G. Plotkin. Dynamic typing in a statically-typed language. In Proceedings of POPL, 1989. Google ScholarGoogle ScholarDigital LibraryDigital Library
  2. A. Ahmed, R. B. Findler, J. G. Siek, and P. Wadler. Blame for all. In Proceedings of POPL, 2011. Google ScholarGoogle ScholarDigital LibraryDigital Library
  3. E. Allende, J. Fabry, and É. Tanter. Cast insertion strategies for gradually-typed objects. In Proceedings of DLS, 2013. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. E. Allende, J. Fabry, R. Garcia, and É. Tanter. Confined gradual typing. In Proceedings of OOPSLA, 2014. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. C. Anderson, P. Giannini, and S. Drossopoulou. Towards type inference for JavaScript. In Proceedings of ECOOP, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  6. G. Bierman, M. Abadi, and M. Torgersen. Understanding TypeScript. In Proceedings of ECOOP, 2014.Google ScholarGoogle Scholar
  7. R. Cartwright and M. Fagan. Soft typing. In Proceedings of PLDI, 1991. Google ScholarGoogle ScholarDigital LibraryDigital Library
  8. R. Chugh, D. Herman, and R. Jhala. Dependent types for JavaScript. In Proceedings of OOSLA, 2012. Google ScholarGoogle ScholarDigital LibraryDigital Library
  9. M. Fagan. Soft Typing: An Approach to Type Checking for Dynamically Typed Languages. PhD thesis, 1990. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. A. Feldthaus and A. Müller. Checking correctness of TypeScript interfaces for JavaScript libraries. In Proceedings of OOPSLA, 2014. Google ScholarGoogle ScholarDigital LibraryDigital Library
  11. A. Guha, C. Saftoiu, and S. Krishnamurthi. The essence of JavaScript. In Proceedings of ECOOP, 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  12. A. Guha, C. Saftoiu, and S. Krisnamurthi. Typing local control and state using flow analysis. In Proceedings of ESOP, 2011. Google ScholarGoogle ScholarDigital LibraryDigital Library
  13. F. Henglein and J. Rehof. Safe polymorphic type inference for Scheme: Translating Scheme to ML. In Proceedings of FPCA, 1995. Google ScholarGoogle ScholarDigital LibraryDigital Library
  14. D. Herman, A. Tomb, and C. Flanagan. Space-efficient gradual typing. Higher Order Symbol. Comput., 2010. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. B. S. Lerner, J. G. Politz, A. Guha, and S. Krishnamurthi. TeJaS: Retrofitting type systems for JavaScript. In Proceedings of DLS, 2013. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Z. Luo. Coercive subtyping. J. Log. Comput., 9 (1): 105--130, 1999.Google ScholarGoogle ScholarCross RefCross Ref
  17. M. S. Miller, M. Samuel, B. Laurie, I. Awad, and M. Stay. Caja: Safe active content in sanitized JavaScript. Unpublished paper, 2007.Google ScholarGoogle Scholar
  18. A. Rastogi, N. Swamy, C. Fournet, G. Bierman, and P. Vekris. Safe & efficient gradual typing for TypeScript, MSR-TR-2014--99, 2014.Google ScholarGoogle Scholar
  19. G. Richards, F. Z. Nardelli, C. Rouleau, and J. Vitek. Types you can count on. Unpublished paper, 2014.Google ScholarGoogle Scholar
  20. J. G. Siek and W. Taha. Gradual typing for functional languages. In Proceedings of Scheme and functional programming workshop, 2006.Google ScholarGoogle Scholar
  21. J. G. Siek and W. Taha. Gradual typing for objects. In Proceedings of ECOOP, 2007. Google ScholarGoogle ScholarDigital LibraryDigital Library
  22. J. G. Siek and M. M. Vitousek. Monotonic references for gradual typing. Unpublished paper, 2013.Google ScholarGoogle Scholar
  23. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  24. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  25. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  26. P. Thiemann. Towards a type systems for analyzing JavaScript programs. In Proceedings of ESOP, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  27. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  28. S. Tobin-Hochstadt and M. Felleisen. Interlanguage migration: from scripts to programs. In Proceedings of DLS, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  29. S. Tobin-Hochstadt and M. Felleisen. The design and implementation of Typed Scheme. In Proceedings of POPL, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  30. 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 ScholarGoogle Scholar
  31. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  32. P. Wadler and R. B. Findler. Well-typed programs can't be blamed. In Proceedings of ESOP, 2009. Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  34. 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 ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. Safe & Efficient Gradual Typing for TypeScript

            Recommendations

            Comments

            Login options

            Check if you have access through your login credentials or your institution to get full access on this article.

            Sign in

            Full Access

            • Published in

              cover image ACM SIGPLAN Notices
              ACM SIGPLAN Notices  Volume 50, Issue 1
              POPL '15
              January 2015
              682 pages
              ISSN:0362-1340
              EISSN:1558-1160
              DOI:10.1145/2775051
              • Editor:
              • Andy Gill
              Issue’s Table of Contents
              • cover image ACM Conferences
                POPL '15: Proceedings of the 42nd Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages
                January 2015
                716 pages
                ISBN:9781450333009
                DOI:10.1145/2676726

              Copyright © 2015 ACM

              Publisher

              Association for Computing Machinery

              New York, NY, United States

              Publication History

              • Published: 14 January 2015

              Check for updates

              Qualifiers

              • research-article

            PDF Format

            View or Download as a PDF file.

            PDF

            eReader

            View online with eReader.

            eReader
            About Cookies On This Site

            We use cookies to ensure that we give you the best experience on our website.

            Learn more

            Got it!