DEV Community

Discussion on: Convince me that types are awesome

Collapse
 
patrick profile image
Patrick Ziegler

One great example I heard just today on the corecursive podcast is that some typos in your JS are only caught at runtime if at all:

var x = {'prop': "text"};
var y = x.porp;

y is undefined here, you only notice this once you get a strange result somewhere or, if you're lucky, get a TypeError: y is undefined. You basically need full test coverage to be sure this isn't happening before deploying.

In a typed language like C you don't have this problem:

struct foo {
  char* prop;
}

...

struct foo x = {"text"};
char* y = x.porp;

This will immediately give you a compiler error:

error: ‘struct foo’ has no member named ‘porp’; did you mean ‘prop’?