Official Website: www.typescriptlang.org
Documentation: https://www.typescriptlang.org/docs

TypeScript is a superset of JavaScript that adds static typing to the language. This means you can define the types of variables, function parameters, and return values, which helps catch errors during development before the code runs. TypeScript compiles to plain JavaScript, making it compatible with any environment where JavaScript runs, such as browsers or Node.js. Developed and maintained by Microsoft, TypeScript is open-source and widely used in modern web development, especially in frameworks like Angular and React.
TypeScript offers several benefits that make it a valuable tool for developers, particularly for those working on medium to large projects:
However, TypeScript requires compiling, which can add time, and writing type annotations may feel like extra work for small projects. For beginners, the learning curve is gentle if you already know JavaScript.
Using TypeScript involves a few straightforward steps:
To start using TypeScript, you need to install its compiler, known as tsc. Here’s how to set it up:
node -v in your terminal.npm install -g typescripttsc -vnpm install typescript --save-devnpx tsc --initThe tsconfig.json file allows you to specify options like "target": "es2016" for the JavaScript version or "strict": true for stricter type checking. This file is essential for managing larger projects.
TypeScript’s type system is its core feature, allowing you to define the types of data your variables can hold. Below are the most common types, with examples to illustrate their use.
| Type | Description | Example |
|---|---|---|
| string | Represents text data | let name: string = "Alice"; |
| number | Represents numeric values (integers or floats) | let age: number = 30; |
| boolean | Represents true/false values | let isActive: boolean = true; |
| undefined | Represents unassigned variables | let nothing: undefined = undefined; |
| null | Represents intentional absence of value | let empty: null = null; |
These types are used for more complex data structures:
let numbers: number[] = [1, 2, 3];let person: { name: string; age: number } = { name: "Alice", age: 30 }function greet(name: string): string {
  return `Hello, ${name}!`;
}TypeScript can automatically infer types based on initial values, reducing the need for explicit annotations.
let name = "Alice"; // Inferred as stringlet age = 30; // Inferred as numberAllows a variable to hold multiple types.
let value: string | number = "hello";value = 42; // AllowedArrays with a fixed number of elements of specific types.
let person: [string, number] = ["Alice", 30];Disables type checking for a variable, reverting to JavaScript’s flexibility (use sparingly).
let anything: any = "hello";anything = 42; // AllowedCreate reusable type definitions.
type StringOrNumber = string | number;let value: StringOrNumber = "hello";value = 42;Define the shape of objects.
interface Person {
  name: string;
  age: number;
}let person: Person = { name: "Alice", age: 30 }Specify exact values.
let color: "red" | "blue" = "red";Create reusable components that work with multiple types.
function identity<T>(arg: T): T {
  return arg;
}let output = identity<string>("myString");Define a set of named constants.
enum Direction {
  Up,
  Down,
  Left,
  Right
}let dir: Direction = Direction.Up;Let’s walk through creating a basic TypeScript project:
mkdir my-typescript-projectcd my-typescript-projectnpm init -ynpm install typescript --save-devnpx tsc --initfunction greet(name: string): string {
  return `Hello, ${name}!`;
}
console.log(greet("TypeScript"));npx tsc. node app.js. Hello, TypeScript! in the console.TypeScript compiles to JavaScript, which can then be included in an HTML file for web applications. Here’s an example:
function greet(name: string): string {
  return `Hello, ${name}!`;
}
console.log(greet("TypeScript"));npx tsc app.ts --outFile app.js<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>TypeScript Example</title>
</head>
<body>
  <script src="app.js"></script>
</body>
</html>Hello, TypeScript!TypeScript is a powerful tool that enhances JavaScript by adding static typing, making your code more robust and easier to maintain. By catching errors early, improving readability, and supporting modern development practices, TypeScript is an excellent choice for beginners and experienced developers alike. With the steps and examples provided, you can start building your own TypeScript projects and explore its advanced features as you grow.