Beginner's Guide to TypeScript

Beginner's Guide to TypeScript

·

4 min read

If you're new to TypeScript and want to know more then you've come to the right place!

TypeScript was created to relieve some of the pain-points of JavaScript. Using TypeScript can help you avoid bugs you would otherwise encounter when writing regular JS. It is also Open Source, and has wide adoption and support.

This guide includes everything you need to get started with TypeScript, and nothing you don't.

What is TypeScript?

TypeScript is a programming language that is a typed superset of JavaScript. That means that it contains all of the functionality of JavaScript, but with additional features that make it more powerful and easier to use. TypeScript is compiled to plain JavaScript, so it can be used in any JavaScript environment.

Why Use TypeScript?

TypeScript is a great way to improve your code. It can catch errors and help you write more maintainable code.

TypeScript offers developers a number of benefits, including:

  1. TypeScript provides static type checking. This can help you find bugs in your code more easily.

  2. TypeScript code can be compiled to JavaScript code that runs on any browser or platform. The compiling process also allows developers to catch errors before runtime.

  3. TypeScript comes with a number of tools that make it easy to work with, including an interactive shell and a compiler.

Getting Started

To get started with TypeScript, you will need to install it. The easiest way to do this is through npm.

Once you have TypeScript installed, you can compile TypeScript files using the tsc command. This will generate a corresponding JavaScript file.

tsc filename.ts

You can also use the -w flag to watch for changes and automatically recompile your files.

tsc -w filename.ts

Basic Types

TypeScript offers a number of basic types that you can use to annotate your variables.

let foo: string = "bar";
let baz: number = 42;
let qux: boolean = true;

You can also use the any type if you don't want to annotate your variables, though it's not recommended.

let foo: any = "bar";
let baz: any = 42;
let qux: any = true;

Interfaces

Interfaces are a powerful way to define contracts in your code. They can be used to define the shape of objects and functions.

interface Point {
  x: number;
  y: number;
}

let p: Point = { x: 0, y: 0 };

You can also use interfaces to define the types of function parameters and return values.

interface Point {
  x: number;
  y: number;
}

function addPoints(p1: Point, p2: Point): Point {
  return { x: p1.x + p2.x, y: p1.y + p2.y };
}

let p1: Point = { x: 0, y: 0 };
let p2: Point = { x: 1, y: 1 };

let p3: Point = addPoints(p1, p2);

Classes

Classes are a way to encapsulate data and behavior. They can be used to define types and create objects.

class Point {
  x: number;
  y: number;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }

  add(p: Point) {
    return new Point(this.x + p.x, this.y + p.y);
  }
}

let p1: Point = new Point(0, 0);
let p2: Point = new Point(1, 1);

let p3: Point = p1.add(p2);

Modules

Modules are a way to organize your code. They can be used to define types and encapsulate behavior.

module MyModule {
  export class Point {
    x: number;
    y: number;

    constructor(x: number, y: number) {
      this.x = x;
      this.y = y;
    }

    add(p: Point) {
      return new Point(this.x + p.x, this.y + p.y);
    }
  }
}

let p1: MyModule.Point = new MyModule.Point(0, 0);
let p2: MyModule.Point = new MyModule.Point(1, 1);

let p3: MyModule.Point = p1.add(p2);

Decorators

Decorators are a way to add metadata to your code. They can be used to annotate classes, properties, and methods.

@Component({
  selector: 'my-component',
  template: '<div>Hello, world!</div>'
})
class MyComponent {}

That's All, Folks!

That's all you need to know to get started with TypeScript. For more information, check out the TypeScript Handbook.

Let me know in the comments if this was helpful or if you have any questions.

Be sure to follow me for more like this!

Did you find this article valuable?

Support trav by becoming a sponsor. Any amount is appreciated!