5 good reasons to adopt TypeScript in your frontend JS application.

5 good reasons to adopt TypeScript in your frontend JS application.

 1)  Typescript is a superset of JavaScript you can just write JavaScript code which is valid TypeScript (TS) code, you can mix and match but you would lose out not making use of the type system. So you can start straight away.

You might say: “Ok I think it’s worth exploring TS but that’s just for Angular right?”, nope. You can use TS with any JS project including Node or React apps, take a look at these repos from Microsoft:

https://github.com/Microsoft/TypeScript-Node-Starter

https://github.com/Microsoft/TypeScript-React-Starter

In particular for React, I think it help make sense of the Component Architecture it uses. Consider the following snippet from Microsoft

import * as React from "react";

export interface HelloProps { compiler: string; framework: string; }

export const Hello = (props: HelloProps) => <h1>Hello from {props.compiler} and {props.framework}!</h1>;

In a typical React component you handle "props" passed from "outside". React does not have Dependency Injection: when a component depends on a function or a value from another component, you can pass the function or value in as a `prop`. But the problem is that most of the time you just have this "props" but don'e have a clue what they contain, it's not clear from reading the code at a glance.

In the snippet above you have an interface for the props handled by the Hello component. You can then easily navigate the code to know what that interface represents.


2)   You write less code and more readable. You will be able to better manage a large codebase, and benefit from language Inheritance for better reusable code. Even if you are a JavaScript purist and hate frameworks, but prefer to write your own libraries, you’ll find TS can help you a lot to give a sense to your API design.

3)   Gets some quick feedback with compilation errors. Type safety is obviously the greatest feature of TypeScript, just make the most of it. Just a very basic example:

interface Foo {

        bar: number;

        bas: string;

     }



     let foo = {} as Foo;

     foo.bar = 123;

     foo.bas = "Hello World";



     // later in the codebase:

     foo.bar = 'Hello Stranger'; // Error: You probably misspelled `bas` as  `bar`, cannot assign string to number

     }

"Don't trust names. Trust types. Types don't lie. Types are your friends!"

[Quote: P.I.Soumont - Functional Programming in Java 8]

4)   You can use ES6 features (or even ES7) without having to worry about browser compatibility (see: ES6 browser compatibility table: https://kangax.github.io/compat-table/es6).

As you might know ES6 is a significant update to the JavaScript language  (see “6 reasons Web developers need to learn JavaScript ES6 now”), although some browser are still behind. In fact you set the TypeScript 'transpiler' to target ES5 and forget about most of your browser incompatibilities.

5)  There is some pretty good tooling supporting TS, like Visual Studio Code and WebStorm IDE, so you'll benefit from intelli-sense, code completion. An example below:

So I really hope you'll consider TS in your next JS application. I think it's about time people take it into consideration a bit more especially in the Enterprise environment where developers tends to build custom JS libraries a lot but they often lack of structure / documentation. Static typing wins here because it gives you a bit of help self documenting code with an appropriate function signature.

要查看或添加评论,请登录

Riccardo Noviello的更多文章

  • Pros and Cons of using individual metrics for Software Developers, using Basketball analogies

    Pros and Cons of using individual metrics for Software Developers, using Basketball analogies

    As a basketball fan (the NBA specifically) I often draw comparisons between Basketball teams and IT teams in a variety…

  • Getting Started with IBM MQ and Spring Boot [Book 2020]

    Getting Started with IBM MQ and Spring Boot [Book 2020]

    For Java developers working with Spring Boot and IBM MQ technologies, get your copy of the book: "Getting Started with…

    1 条评论
  • Writing ES6 JS straight out the box

    Writing ES6 JS straight out the box

    We all know ES6 brings a lot of new features to the JavaScript language, although they are not fully supported by most…

  • Continuous Delivery with Heroku (PaaS)

    Continuous Delivery with Heroku (PaaS)

    Heroku is a platform as a service (PaaS) based on AWS (IaaS), and it's very easy to use. When it comes to choose…

  • Hello Modules (Java 9)

    Hello Modules (Java 9)

    I have recently written about the new features included in the next release of Java (https://www.linkedin.

    1 条评论
  • Java 9 Early Access Release

    Java 9 Early Access Release

    We are very exited about the soon to be release version 9 of the JDK. There are some major improvements which shift the…

  • The Case for Java 8 Optional

    The Case for Java 8 Optional

    I have been working with Java 8 for a while now, and as many other developers I have been moaning at first about some…

    2 条评论
  • How to delete sensitive information from GitHub

    How to delete sensitive information from GitHub

    So what happens when you realise you made a (common) mistake by committing a secret to your Git repository? Well as you…

    4 条评论

社区洞察

其他会员也浏览了