TypeScript or Babel?
TypeScript and Babel are both transpilers
A transpiler takes code from one langauge and converts it into another language. This is similar to a compiler used with a compiled language (like Java or Objective-C), the key difference being the level of abstraction. A transpiler converts between two languages with similar levels of abstraction whereas a compiler converts a higher level of abstraction down to a lower level.
Both TypeScript and Babel are transpilers that take ES6 input and produce the ES5 equivalent. Developers write apps using ES6 or TypeScript and then transpile the code into ES5. The output files are then referenced directly by the application at runtime.
TypeScript is a type checker, Babel is not
var myString = "hello"
return myString * 2;
This will return NaN at runtime since a string can't be multiplied by 2. With TypeScript, we would define our myString variable as follows:
let myString: string = "hello"
Notice how we use : string to explicitly define the data type for our variable myString. Now when we try and transpile our TypeScript file it will throw an error. The transpiler catches us trying to multiply a string by 2 before runtime, effectively catching the error at compile time.
- Early detection of programming mistakes
- Prevents runtime failures
- Easier readability
- Easier to maintain
Sure, type checking adds some extra steps but it keeps your code more organized and helps catch bugs faster (especially for larger applications).
Is TypeScript or Babel right for you?
Both TypeScript and Babel allow you to write your apps using the ES6 standard. This is important since it's only a matter of time before ES6 completely takes over. If you’re operating in an enterprise environment then TypeScript may help you cross the T's and dot the I's. If you were built on ES5 and have no Java experience, then using Babel as a transpiler should be sufficient for your development needs.