Towards Type Inference for JavaScript

  • Christopher Anderson
  • Paola Giannini
  • Sophia Drossopoulou
Part of the Lecture Notes in Computer Science book series (LNCS, volume 3586)


Object-oriented scripting languages like Javascript and Python are popular partly 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 so usually dynamic typing is used. Consequently, errors such as access to non-existent members are not detected until runtime.

We first develop a formalism for an object based language, JS0 with features from Javascript, including dynamic addition of fields and updating of methods. We give an operational semantics and static type system for JS0using structural types. Our types allow objects to evolve in a controlled manner by classifying members as definite or potential.

We define a type inference algorithm for JS0 that is sound with respect to the type system. If the type inference algorithm succeeds, then the program is typeable. Therefore, programmers can benefit from the safety offered by the type system, without the need to write explicitly types in their programs.


Type System Type Variable Function Type Operational Semantic Object Type 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Abadi, M., Cardelli, L.: A Theory of Objects. Springer, New York (1996)zbMATHGoogle Scholar
  2. 2.
    Agesen, O.: The Cartesian Product Algorithm: Simple and Precise Type Inference of Parametric Polymorphism. In: Olthoff, W. (ed.) ECOOP 1995. LNCS, vol. 952, pp. 2–26. Springer, Heidelberg (1995)Google Scholar
  3. 3.
    Agesen, O., Palsberg, J., Schwartzbach, M.I.: Type inference of SELF: Analysis of objects with dynamic and multiple inheritance. Softw., Pract. Exper. 25(9), 975–995 (1995)CrossRefGoogle Scholar
  4. 4.
    Amadio, R.M., Cardelli, L.: Subtyping recursive types. ACM Transactions on Programming Languages and Systems 15(4), 575–631 (1993)CrossRefGoogle Scholar
  5. 5.
    Anderson, C., Barbanera, F., Dezani-Ciancaglini, M., Drossopoulou, S.: Can addresses be types (a case study: objects with delegation). In: WOOD 2003. ENTCS, vol. 82. Elsevier, Amsterdam (2003)Google Scholar
  6. 6.
    Anderson, C., Giannini, P.: Type checking for javascript. In: WOOD 2004. WOOD of ENTCS. Elsevier, Amsterdam (2004), Google Scholar
  7. 7.
    Bono, V., Fisher, K.: An Imperative, First-Order Calculus with Object Extension. In: Jul, E. (ed.) ECOOP 1998. LNCS, vol. 1445, pp. 462–497. Springer, Heidelberg (1998)CrossRefGoogle Scholar
  8. 8.
    Bruce, K., Schuett, A., van Gent, R.: PolyTOIL: A type-safe polymorphic object-oriented language. In: Olthoff, W. (ed.) ECOOP 1995. LNCS, vol. 952, pp. 27–51. Springer, Heidelberg (1995)Google Scholar
  9. 9.
    Damiani, F., Giannini, P.: Alias types for environment aware computations. In: WOOD 2003. ENTCS, vol. 82. Elsevier, Amsterdam (2003)Google Scholar
  10. 10.
    Eifrig, J., Smith, S.F., Trifonov, V.: Sound polymorphic type inference for objects. In: Proc. Object-Oriented Programming Systems, Languages, and Applications (OOPSLA 1995), pp. 169–184. ACM Press, New York (1995)CrossRefGoogle Scholar
  11. 11.
    Le Hors, A., et al.: Document Object Model (DOM) Level 3 Core Specification. Technical report (1998),
  12. 12.
    Agesen, O., et al.: The SELF 4.0 Programmer’s Reference Manual (1995),
  13. 13.
    Fisher, K.: Type Systems for Object-Oriented Programming Languages. PhD thesis, Stanford University, Available as Stanford Computer Science Technical Report number STAN-CS-TR-98-1602 (1996)Google Scholar
  14. 14.
    Fisher, K., Honsell, F., Mitchell, J.C.: A Lambda Calculus of Objects and Method Specialization. Nordic Journal of Computing 1(1), 3–37 (1994); A preliminary version appeared In: Proc. of IEEE Symp. LICS 1993 (1993) zbMATHMathSciNetGoogle Scholar
  15. 15.
    Flanagan, D.: JavaScript - The Definitive Guide. O’Reilly, Sebastopol (1998)Google Scholar
  16. 16.
    : ECMAScript Language Specification. ECMA International. ECMA-262, 3rd edn. (december 1999),
  17. 17.
    Morrisett, G., Walker, D., Crary, K., Glew, N.: From system f to typed assembly language. ACM Trans. Program. Lang. Syst. 21(3), 527–568 (1999)CrossRefGoogle Scholar
  18. 18.
    Oxhoj, N., Palsberg, J., Schwartzbach, M.I.: Making type inference practical. In: Lehrmann Madsen, O. (ed.) ECOOP 1992. LNCS, vol. 615, pp. 329–349. Springer, Heidelberg (1992)CrossRefGoogle Scholar
  19. 19.
    Hill, W., Olthoff, W., Canning, P., Cook, W., Mitchell, J.C.: F-bounded polymorphism for object-oriented programming. In: Proc. Conf. on Functional Programming Languages and Computer Architecture, pp. 273–280. ACM Press, New York (1989)Google Scholar
  20. 20.
    Palsberg, J.: Efficient inference of object types. Inf. Comput. 123(2), 198–209 (1995)zbMATHCrossRefMathSciNetGoogle Scholar
  21. 21.
    Palsberg, J., Schwartzbach, M.I.: Object-oriented type inference. In: Meyrowitz, N. (ed.) Proceedings of the Conference on Object-Oriented Programming Systems, Languages, and Applications (OOPSLA), vol. 26. ACM Press, New York (1991)Google Scholar
  22. 22.
    Palsberg, J., Zhao, T.: Type inference for record concatenation and subtyping. Inf. Comput. 189(1), 54–86 (2004)zbMATHCrossRefMathSciNetGoogle Scholar
  23. 23.
    Salib, M.: Static Type Inference (for Python) with Starkiller (2004),
  24. 24.
    Thiemann, P.: Towards a type system for analyzing javascript programs. In: Sagiv, M. (ed.) ESOP 2005. LNCS, vol. 3444, pp. 408–422. Springer, Heidelberg (2005)CrossRefGoogle Scholar
  25. 25.
    Wang, T., Smith, S.F.: Precise constraint-based type inference for java. In: Knudsen, J.L. (ed.) ECOOP 2001. LNCS, vol. 2072, pp. 99–117. Springer, Heidelberg (2001)CrossRefGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2005

Authors and Affiliations

  • Christopher Anderson
    • 1
  • Paola Giannini
    • 2
  • Sophia Drossopoulou
    • 1
  1. 1.Department of ComputingImperial College LondonLondonU.K
  2. 2.Dipartimento di InformaticaUniversità del Piemonte OrientaleAlessandriaItaly

Personalised recommendations