DEV Community

aohibbard
aohibbard

Posted on

Getting Into TypeScript: What is a Dynamically Typed Language?

Take a look at job postings out there, and you'll see a lot of mentions of TypeScript. It doesn't take long to glean that TypeScript exists in relation to JavaScript, but what exactly is that relation?

TypeScript was developed at Microsoft and first released in 2012. TypeScript is sort of a superset of JavaScript, adding a type system on top of JavaScript. This description from the TypeScript Github repository is not a bad starting point:

TypeScript is a language for application-scale JavaScript. TypeScript adds optional types to JavaScript that support tools for large-scale JavaScript applications for any browser, for any host, on any OS. TypeScript compiles to readable, standards-based JavaScript.

To get at the core difference, it is important to understand what a dynamically typed language is, and what a statically typed language is. Dynamically typed languages include Perl, Ruby, Python, PHP, JavaScript—big languages in full-stack web development and the core languages we see being taught in bootcamps. Statically typed languages include C, C++, Java, Rust, Go, Scala.

Statically typed languages either work through type inference or force you to declare the type of a variable (e.g. string, integer) in writing the code so it is known at compilation. Dynamically typed languages do not require this. Oracle identifies two key differences

First, dynamically-typed languages perform type checking at runtime, while statically typed languages perform type checking at compile time. This means that scripts written in dynamically-typed languages...can compile even if they contain errors that will prevent the script from running properly (if at all). If a script written in a statically-typed language (such as Java) contains errors, it will fail to compile until the errors have been fixed.

Second, statically-typed languages require you to declare the data types of your variables before you use them, while dynamically-typed languages do not.

Dynamically typed languages offer a bit more speed for the coder, sparing you the step of type declaration. As mentioned above, your code will compile without errors in a dynamically typed language. It's the sort of logic that allows us to do get something like "1" + 1 = "11" or "alert" + [1,2] = "alert1,2"in JavaScript. Although we can sometimes use this to our advantage, it can also result in errors.

TypeScript then sits on top of JavaScript and allows a bit more control over things. Consider this example from the TypeScript documentation.

function greeter(person: string) {
  return "Hello, " + person;
}

let user = [0, 1, 2];

document.body.textContent = greeter(user);
Argument of type 'number[]' is not assignable to parameter of type 'string'.
Enter fullscreen mode Exit fullscreen mode

What we see is by declaring the type of the person argument, anything passed in that is not a string will yield an error. It's a small addition but a huge amount of control added to your programming.

I'm a big fan of knowing the history of things, and this video of Jonathan Turner talking about TypeScript in 2013 is a great resource and starting place.

Top comments (0)