News for hackers. Stuff that matters.
Re:Type Checking in JavaScript by marijn on Wednesday November 06, 2013 @06:45AM (#6682198)
Well, let's see

    var mostRecent = '';

    if (type(c.messages) === 'array' ||
        type(c.messages[0]) === 'object' ||
        type(c.messages[0].text) === 'string') {
        mostRecent = c.messages[0].text.substring(0, 30);
    }

    return name + ': ' + mostRecent;
.. apart from the fact that he probably meant && instead of || in that condition, we've also now got a system that silently swallows errors. Bugs can lurk in it without any more symptoms than producing empty strings where more interesting strings were expected.

I much prefer my systems to crash when a programmer made an error, and do so noisily and detectably, so that the problem can be fixed. I also prefer the actual logic of my code to make up more than 20% of the lines. If you're going to add boilerplate type checks all over, the signal-to-noise ratio gets really bad.

So don't do this. Use Typescript if you want to check types.

Re:Type Checking in JavaScript by annnnd on Wednesday November 06, 2013 @06:53AM (#6682218)
Agree with you 100%.

One could use this system for asserts though:

    assert(type.isObject({})));
    assert(type.isNumber(NaN)); // fails
    assert(type.isElement(document.createElement('div')));
    assert(type.isRegExp(/abc/);
This could increase readability (because it documents the data type expectations) and would also break on some errors as soon as possible.
Re:Type Checking in JavaScript by ghiculescu on Wednesday November 06, 2013 @08:00AM (#6682441)
It's still a lot of code being run without real benefit. Use Typescript if you really want static types, write sufficient unit tests either way, and you don't need this.
Re:Type Checking in JavaScript by rimo on Wednesday November 06, 2013 @08:42AM (#6682589)
Static types and unit tests don't solve or catch all problems. I'm thinking about event-driven messaging systems, which is pretty common in js; Static typing will not be of any help in that situation.
Re:Type Checking in JavaScript by danbruc on Wednesday November 06, 2013 @10:42AM (#6683211)
Sufficiently advanced type systems can enforce an enormous range of constraints. You can for example create a type for trees enforcing that the tree must be balanced; code that would unbalance a tree will just not compile.
Re:Type Checking in JavaScript by embwbam on Wednesday November 06, 2013 @09:20AM (#6682758)
You can use a different observer pattern sometimes that can be caught by static typing, like Signals
Re:Type Checking in JavaScript by annnnd on Friday November 08, 2013 @02:34AM (#6694996)
Sure - and you can test types in your unit tests using the OP's technique. :)
Re:Type Checking in JavaScript by jonbretman on Wednesday November 06, 2013 @06:57AM (#6682232)
Firstly good spot on the '||' instead of an '&&'. Will fix that now.

Secondly I completely agree about wanting a program to crash noisily and detectably in development, but in production I would prefer that the error is handled and the user can continue to use the app.

Finally I also agree that if type checking is really important to you, a compile to JS language like TypeScript or Dart are the best options. The techniques / approach I am discussing in this article are really for people who already have a large vanilla JS code base or want to learn a bit more about types in JavaScript.

Re:Type Checking in JavaScript by vladiliescu on Wednesday November 06, 2013 @07:09AM (#6682270)
Just wanted to add that TypeScript is backwards compatible with JS, so having a large vanilla JS code base doesn't really matter - it's already valid TypeScript code, so you can go and start adding type annotations and whatnot.
Re:Type Checking in JavaScript by nfoz on Wednesday November 06, 2013 @12:44PM (#6683997)
> in production I would prefer that the error is handled and the user can continue to use the app.

Maybe that's ok if it's a game or something, but this is a dangerous strategy. How do you know it's ok to use the app if there's an error? You have to very carefully isolate and control where your errors can be, and I'm not sure this approach allows you to do that.

Re:Type Checking in JavaScript by hrjet on Wednesday November 06, 2013 @07:10AM (#6682273)
Typescript is a superset of Javascript. So any vanilla JS code base can be easily ported to Typescript.
Re:Type Checking in JavaScript by spion on Wednesday November 06, 2013 @11:18AM (#6683427)
Typescript has its own issues:

  interface IA { }
  interface IB { foo(): void; }
   
  function fn(passedFn: (a: IA) => void) {
    passedFn({}); 
  }
 
  // compiles but throws TypeError at runtime!
  fn((b: IB) => b.foo());