Skip to main content

Command Palette

Search for a command to run...

Learn Typescript basics

Typescript for javascript devs

Published
2 min read
Learn Typescript basics
S

Highly motivated and detail-oriented software engineering student with a passion for solving complex problems and developing innovative software solutions. Skilled in programming languages such as Java, Python, and JavaScript, with experience in software development methodologies and project management. Strong team player with excellent communication and interpersonal skills, and a track record of delivering high-quality software solutions on time and within budget.

TypeScript is a popular programming language that is a superset of JavaScript. It provides additional features such as static typing, which can help you catch errors before runtime and make your code more robust. Here's an overview of the basics of TypeScript:

  1. Installation: To use TypeScript, you need to have Node.js and npm (Node Package Manager) installed on your system. Once you have Node.js installed, you can install TypeScript by running the following command in your terminal:
npm install -g typescript
  1. Basic syntax: TypeScript has a syntax that is very similar to JavaScript, but with some additional features. For example, you can define variables with a specific type, like this:
let name: string = "John";
let age: number = 30;
let isEmployed: boolean = true;

In this example, we're defining variables of type string, number, and boolean. TypeScript also supports other types such as any, void, null, and undefined.

  1. Functions: In TypeScript, you can define functions with a specific type signature. For example, you can define a function that takes two numbers as arguments and returns a number:
function addNumbers(x: number, y: number): number {
  return x + y;
}
  1. Classes: TypeScript supports class-based object-oriented programming. Here's an example of a class definition in TypeScript:
class Person {
  private name: string;
  private age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  public sayHello(): void {
    console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
  }
}

In this example, we're defining a class called Person with a constructor that takes a name and an age argument. We're also defining a method called sayHello that logs a message to the console.

  1. Interfaces: TypeScript supports interfaces, which allow you to define a shape for an object. For example, you can define an interface for a person like this:
interface Person {
  name: string;
  age: number;
  isEmployed: boolean;
}

In this example, we're defining an interface called Person that has three properties: name, age, and isEmployed.

  1. Compiling TypeScript: Once you've written your TypeScript code, you'll need to compile it into JavaScript before you can run it in a browser or on a server. You can do this using the tsc command, like this:
tsc myfile.ts

This will compile myfile.ts into myfile.js.

These are just some of the basics of TypeScript, but there's a lot more to learn! I recommend checking out the TypeScript documentation for more information: https://www.typescriptlang.org/docs/