Skip to main content

TypeScript Basics

TypeScript = JavaScript with static types


What is TypeScript?

"Typed superset of JavaScript"

TypeScript is a typed superset of JavaScript that:

  • Compiles to JavaScript
  • Adds static type checking
  • Provides better tooling (IntelliSense)
  • Catches errors at compile time

"TypeScript is a typed superset of JavaScript that compiles to plain JavaScript, adding static type checking and improved developer experience."


Why TypeScript?

Catch errors early + better tooling

Benefits:

  • Type safety: Catch errors before runtime
  • Better IDE support: Autocomplete, refactoring
  • Self-documenting: Types serve as documentation
  • Easier refactoring: Safer code changes

"TypeScript helps catch errors at compile time, improves IDE support, and makes code more maintainable."


Basic Types

Primitives + any + void + never

let name: string = "John";
let age: number = 30;
let isActive: boolean = true;
let data: any = "anything";
let nothing: void = undefined;
let never: never; // never returns

"TypeScript provides type annotations for primitives, any for dynamic types, void for no return, and never for unreachable code."


Type Inference

TypeScript infers when you don't specify

let x = 10; // inferred as number
let y = "hello"; // inferred as string

"TypeScript can infer types automatically, so explicit annotations aren't always needed."


Interfaces

Interfaces = shape of objects

interface User {
name: string;
age: number;
email?: string; // optional
}

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

"Interfaces define the shape of objects and can include optional properties."


Types vs Interfaces

Interfaces extend, types combine

FeatureInterfaceType
Extends
Merges
Unions
Intersections

"Interfaces can be merged and extended, while types support unions and are more flexible for complex types."


Generics

Generics = reusable type parameters

function identity<T&gt(arg: T): T {
return arg;
}

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

"Generics allow creating reusable components that work with multiple types."


Union Types

Union = this OR that

type Status = "pending" | "approved" | "rejected";
type ID = string | number;

"Union types allow a value to be one of several types."


9️⃣ Type Guards

Narrow types with checks

function isString(value: unknown): value is string {
return typeof value === "string";
}

if (isString(x)) {
// x is string here
}

"Type guards narrow types within conditional blocks for type safety."


Enums

Enums = named constants

enum Status {
Pending,
Approved,
Rejected,
}

"Enums provide a way to define a set of named constants."


Utility Types

Built-in type transformations

Common utilities:

  • Partial&lt;T&gt - all properties optional
  • Required&lt;T&gt - all properties required
  • Pick<T, K> - select properties
  • Omit<T, K> - exclude properties
  • Readonly&lt;T&gt - make all readonly

"TypeScript is a typed superset of JavaScript that adds static type checking. It provides interfaces for object shapes, generics for reusable types, union types for flexibility, and type guards for narrowing. TypeScript compiles to JavaScript and catches errors at compile time, improving code quality and developer experience."


🧠 Ultra-Short Cheat Sheet

Static typing
Type inference
Interfaces (object shapes)
Types vs Interfaces
Generics (reusable)
Union types (A | B)
Type guards
Enums
Utility types
Compiles to JS