Getting Started
Installation
Types & Interfaces
Type Aliases vs Interfaces
Type Narrowing
Generics
Modules & Compilation

TypeScript

A typed layer on JavaScript for safer app-scale code, interfaces, and modern developer tooling.

Getting Started

TypeScript is a superset of JavaScript that adds optional static types. It compiles to plain JavaScript and helps catch errors earlier, improve editor tooling, and document intent.

Installation

bash
npm install --save-dev typescript
npx tsc --init

A minimal tsconfig.json includes:

json
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "strict": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true
  }
}

Types & Interfaces

TypeScript uses type annotations to describe values and enforce contracts at compile time.

ts
// Primitives
let count: number = 10;
let name: string = "RefMe";
let isActive: boolean = true;

// Arrays
const ids: number[] = [1, 2, 3];
const names: Array<string> = ["Alice", "Bob"];

// Objects
interface User {
  id: number;
  name: string;
  email?: string;
}

const user: User = {
  id: 1,
  name: "Alice",
};

Type Aliases vs Interfaces

ts
type Status = "pending" | "success" | "error";

interface Product {
  id: number;
  title: string;
  price: number;
}

Type Narrowing

TypeScript can infer and narrow unions with control flow.

ts
function format(value: string | number) {
  if (typeof value === "string") {
    return value.toUpperCase();
  }
  return value.toFixed(2);
}

Discriminated unions are a powerful pattern for safe branching:

ts
type Shape =
  | { kind: "circle"; radius: number }
  | { kind: "rectangle"; width: number; height: number };

function area(shape: Shape) {
  switch (shape.kind) {
    case "circle":
      return Math.PI * shape.radius ** 2;
    case "rectangle":
      return shape.width * shape.height;
  }
}

Generics

Generics let you write reusable abstractions that preserve type information.

ts
function identity<T>(value: T): T {
  return value;
}

const result = identity<string>("hello");

Common generic patterns:

ts
function mapArray<T, U>(items: T[], transform: (item: T) => U): U[] {
  return items.map(transform);
}

Modules & Compilation

TypeScript supports ES module syntax and works naturally with modern bundlers.

ts
// src/math.ts
export function add(a: number, b: number) {
  return a + b;
}

// src/index.ts
import { add } from "./math";
console.log(add(3, 4));

Compile with npx tsc or use a framework build step. The type system provides safety without changing runtime behavior.

Was this page helpful?
Thank you for your feedback! 🎉
Yash Vardhan
Yash-Pluto

Lead Maintainer

Join the community building the ultimate quick-reference. Contributions are welcome for all categories and languages.

✓Open Source Architecture
✓Community Driven Updates
Open PRs
-
Merged
-
Issues
-
Stars
-

Contributors

Edit Page Contribute

Table of Contents

Getting Started
Installation
Types & Interfaces
Type Aliases vs Interfaces
Type Narrowing
Generics
Modules & Compilation

RefMe is the modern and intuitive way to master tech stacks and build exceptional products.

Product

  • Product Updates
  • Roadmaps
  • Video Tutorials

Resources

  • Customer stories
  • Product docs
  • Philosophy

Company

  • About
  • CareersWE'RE HIRING
  • Open Source
RefMe

© 2026 RefMe

SecurityTerms of servicePrivacy policy