Features of TypeScript

Profile picture for user arilio666

Here are some intresting features of TypeScript:

1. Instant Feedback 

  • JavaScript is a dynamic language where we can do all kinds of crazy things like reference variables that don't exist or work with objects of unknown shape.
  • The code is interpreted by the browser and we won't catch this error until runtime when the browser throws an error.
  • Typescript prevents this from ever happening by extending js with types by providing instant feedback.

2. Superset of JavaScript 

  • The language is a strict superset of Javascript which means when you open a Typescript file you can write plain Javascript with all of its extra features being completely optional.

3. Compiled Language 

  • The reason we get this instant feedback is that Typescript behaves like a compiled language while Javascript is the compilation target.
  • We can run the Typescript compiler using the 'tsc' command.
  • When compiled it'll take the Transcript file and transpile it into Javascript.

    4. Customize Compiler Behavior

    Typescript project will have a ts config file that provides an infinite number of ways to customize the behavior of the compiler.

     

    5. Static Typing 

    • The primary goal of Typescript is to enable static typing. One way it achieves that is by letting us annotate our code with types.
    let appName: string;    //we can take variable followed by annotation and specifying its type thats known as explicit type
    appName=23; //Error
    appName='jacob'  //Correct
    • If we try to initialize the variable with the wrong value we get an error.
    • Alternatively, if we set an initial value it'll implicitly infer the type.
    let appName= 'jacob'; //automatically takes up the type.
    • If we want to change the type after we can just annotate with the 'any' type.
    let appName: any = 'jacob';  
    
    appName=20;  //This way we can opt out of type anytime.