Quiz on AI Interviews Prep Live Training Corporate Training

TypeScript Tutorials.

This tutorial introduces TypeScript, a strongly typed superset of JavaScript. It is designed for developers with basic JavaScript knowledge.

1. What is TypeScript?

TypeScript is a programming language developed by Microsoft that adds static typing to JavaScript. TypeScript code compiles to plain JavaScript.

  • Detects errors at compile time
  • Improves code readability and maintainability
  • Supports modern JavaScript features

2. Installing TypeScript

Install TypeScript using npm:

npm install -g typescript

Check installation:

tsc --version

3. Your First TypeScript Program

Create a file called hello.ts:

let message: string = "Hello, TypeScript!";
console.log(message);

Compile it:

tsc hello.ts

This generates hello.js.

4. Basic Types

let isDone: boolean = true;
let age: number = 25;
let username: string = "Alice";
let numbers: number[] = [1, 2, 3];
TypeScript will throw an error if you assign a wrong type.

5. Functions

function add(a: number, b: number): number {
  return a + b;
}

const result = add(5, 10);

Function return types are optional but recommended.

6. Interfaces

interface User {
  name: string;
  age: number;
  isAdmin: boolean;
}

const user: User = {
  name: "John",
  age: 30,
  isAdmin: false
};

7. Classes

class Person {
  name: string;

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

  greet(): void {
    console.log("Hello, " + this.name);
  }
}

const person = new Person("Sarah");
person.greet();

8. Union Types

let value: string | number;

value = "Hello";
value = 42;

9. Type Aliases

type ID = string | number;

let userId: ID;
userId = 101;
userId = "A123";

10. Conclusion

TypeScript helps you write safer and more scalable JavaScript. It is widely used in frameworks like Angular, React, and Node.js.

Next steps:

  • Generics
  • Enums
  • Modules
  • TypeScript with React