Static vs Dynamic
Typing

Dynamic vs Static Typed

  • It's more of a philosophical question
  • The issue of types predates computers
  • The mathematical problem of ambiguous statements
  • A solution was the notion of types

What we have learned over the last half-century is what not to do.

Clean Architecture by Uncle Bob

Studies

Statically typed languages in general are less defect prone than the dynamically typed ones.

Studies:

  • A Large Scale Study of Programming Languages and Code Quality in Github
  • Software Defect Origin and Removal Methods, by Capers Jones, 2012

Static types addons

Dynamic languages with static types addons:

  • Python - Pyre (Facebook, Instagram)
  • PHP 5 - Hack (for HHVM, Facebook)
  • PHP 7 - Scalar Type Declarations (official)

Advantages of Static Types

  • Enables better tool support
    • safe refactoring in a big codebase
    • better autocompletion
    • code exploration
  • The best form of documentation
    • less verbose than using comments
  • Types make abstractions explicit
  • Provide well defined boundaries between sub-systems

Why TypeScript

  • Popular with developers
  • Developed by  Anders Hejlsberg
  • Great tooling
  • Great 3rd party support
    • code editors, compilers, linters, and so on
  • Very good support for React and JSX
  • Used by big projects
    • Angular 2, Dojo

Types in TypeScript

  • TypeScript is intentionally and strictly a superset of JavaScript
  • TypeScript is optionally typed by default

Types:

  • can be implicit -> inference
  • can be explicit -> by adding type annotations
  • are structural -> checks the structure of a construct
  • can be ambiental
    • means declared in a different place than the code
    • can be added to existing code (like libraries)

Other TypeScript Features

JavaScript code is valid TypeScript code

Features:

  • interfaces
  • enums
  • hybrid types
  • generics
  • union and intersection types
  • access modifiers

Costs

  • there is a learning curve
  • takes time to convert an existing code base
  • have to rely on 3rd party packages for types
  • types can be a bit verbose
  • types can make the code more rigid

Static vs Dynamic ​Typing

By kenjiru

Static vs Dynamic ​Typing

With a quick look at TypeScript

  • 373