Thursday, August 31, 2017

Type checking in javascript has never been easier

Type checking in javascript has never been easier


I love languages which arent strongly typed. It makes software development process very fast and very easy as we need not write huge, complex, unreadable code like in our typed languages. However this can lead to developers worst nightmare: Runtime errors! In languages such as javascript it is not easy to check for type mismatches while in development process. Hence flow was invented!

Flow is one of Facebooks open source projects. It allows developers to check for programming errors in javascript code with very less effort. This is a sample code available on their site:

function foo(x) {
return x * 10;
}
foo(Hello, world!);

Note that a string is passed into the function foo and an arithmetic operation is performed on that string inside it. When flow is run on this code:

01_HelloWorld/hello.js:7:5,17: string
This type is incompatible with
01_HelloWorld/hello.js:4:10,13: number

It points out that the string is incompatible with number. Consider a much much complex code. An error like this can simply be unnoticed until a runtime error is raised.

Source: https://github.com/facebook/flow
Installation: http://flowtype.org/docs/getting-started.html
Examples: http://flowtype.org/docs/five-simple-examples.html


download file now