#01 – Introduction – TypeScript

What is TypeScript?

  • TypeScript is a typed super-set of JavaScript
    • It means that any JavaScript code is a legal TypeScript code
    • Also we can write any JavaScript code in a Typescript file (.ts) without any error
    • It add rules about how different type of values can be used
    • It preserves the run-time behavior of JavaScript code, which means that TypeScript will not throw any “type” errors on run-time
  • TypeScript is a static type checker for JavaScript
    • Detecting errors in code without running is known as static checking
    • Determining the nature or type of error based on the type of values is known as static type checking
    • So TypeScript checks a program for errors before execution based on the type of values
  • So the bottom line is that
    • We can use TypeScript to develop our JavaScript programs
      • TypeScript provides types, classes and OOP concepts
      • TypeScript give details about errors or warnings produced by the code before the execution of the code
      • It highlights the unexpected behavior in code, lowering the chance of bugs
    • Then TypeScript compiler converts the code back to JavaScript code without the TYPES information
//Example Code:

const obj = { width: 10, height: 15 };
const area = obj.width * obj.heigth;

Error: Property ‘heigth’ does not exist on type ‘{ width: number; height: number; }’. Did you mean ‘height’?

Above code will produce this error in TypeScript

Types in TypeScript

  • TypeScript (TS) can infer types from existing JavaScript (JS) variables using their values
    • So TS automatically assign types to the existing JS code
    • Which means that now TS will warn us about warnings or errors in the JS code before execution (Excellent! Not?)
  • TS also allows to define types manually
  • TS provides Unions and Generics to create complex types combining simple types

References:

  • https://www.typescriptlang.org/docs/handbook/typescript-from-scratch.html

Leave a Comment

Your email address will not be published.