Static versus Dynamic Typing
Explore the concepts of static and dynamic typing and how they apply to programming languages. Understand TypeScript's approach to static typing, type inference, and the trade-offs compared to dynamic languages like JavaScript and Ruby. This lesson helps you grasp how static typing aids in error detection, tooling, and code communication.
We'll cover the following...
At the most abstract level, there are two different strategies for dealing with type information in a programing language. These strategies are called static typing and dynamic typing.
Statically typed languages
A language with static types requires that each variable be assigned a type when it is first declared. The language expects to know what the type of a value is and uses that information to constrain at compile time what values can be assigned to that variable.
Different static languages have different requirements for how types are assigned. Some languages, like Java, require the type of every variable to be explicitly stated when the variable is declared. Other languages, like TypeScript, allow for type inference. In TypeScript, if you assign a variable with a typical JavaScript assignment like this:
let x = "hello"
...