TypeScript

By Eric Moynihan

Javascript Suks bro

So don't use it

That's where TypeScript comes in

Benefits of TypeScript

TypeScript is a superset of Javascript. This means it's both compatible with JS code while also supporting more features

Typing system

In TypeScript code, types are defined either implicitly or explicitly.

TypeScript uses type declarations to provide types for javascript code. These are files that have the extension '.d.ts'.


      let a = "a"; // Implicit type of "string".
      let b: string = "b" // Explicit type of "string".
            

index.js:


      function abc(str) {
        return "abc" + str;
      }
            

index.d.ts:


              declare function abc(str: string): string;
            

Interfaces

Abstract classes that are used to define types of methodless objects


      interface A {
        i: number;
        j: string;
      }
      interface J {
        i: number;
      }
      let a: A = {i: 1, j: "1"}; 
      // If the attributes match up then it's type is valid
      // Therefore casting as below is valid
      let b = a as J;
            

Types are resolved using structural subtyping. That means an interface would be treated as something like {a: number} since it's base type is an object with an attribute a of type number.

Now the live demoooooo