Skip to content

You are viewing a free preview of this lesson.

Subscribe to unlock all 12 lessons in this course and every other course on LearningBro.

What is TypeScript and Why Use It

What is TypeScript and Why Use It

TypeScript is a statically typed superset of JavaScript developed and maintained by Microsoft. It adds optional type annotations, interfaces, generics, and other powerful features to JavaScript whilst compiling down to plain JavaScript that runs in any browser or Node.js environment. Since its first public release in 2012, TypeScript has grown to become one of the most popular languages in the world, consistently ranking in the top ten of developer surveys.


A Brief History

  • 2010 — Anders Hejlsberg (creator of C#) begins work on TypeScript at Microsoft
  • 2012 — TypeScript 0.8 is publicly released
  • 2014 — TypeScript 1.0 ships, introducing a stable foundation
  • 2016 — TypeScript 2.0 brings non-nullable types, tagged unions, and the --strict flag
  • 2018 — TypeScript 3.0 introduces project references and unknown type
  • 2020 — TypeScript 4.0 adds variadic tuple types and labelled tuple elements
  • 2023 — TypeScript 5.0 ships with decorators, const type parameters, and performance improvements
  • Today — TypeScript is used by the majority of professional JavaScript developers and adopted by frameworks such as Angular, Next.js, and Deno

Why Choose TypeScript?

1. Catch Errors at Compile Time

TypeScript's type system catches entire categories of bugs before your code ever runs:

function greet(name: string): string {
  return `Hello, ${name}!`;
}

// This would be caught at compile time:
// greet(42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'

Without types, this mistake would silently pass in JavaScript and only surface at runtime — possibly in production.

2. Superior Developer Experience

Modern editors like VS Code use TypeScript's type information to provide:

  • Autocomplete — intelligent suggestions based on types
  • Inline documentation — hover over any symbol for type info
  • Refactoring tools — rename symbols safely across your entire codebase
  • Error highlighting — red squiggles as you type, not after you ship

3. Self-Documenting Code

Types serve as living documentation that never goes stale:

interface User {
  id: number;
  name: string;
  email: string;
  role: 'admin' | 'editor' | 'viewer';
}

Anyone reading this interface immediately understands the shape of a User without hunting through implementation code.

4. Gradual Adoption

You do not need to rewrite your entire JavaScript project. TypeScript allows gradual adoption:

  • Rename .js files to .ts one at a time
  • Use // @ts-check in JavaScript files for lightweight checking
  • Start with strict: false and tighten over time

How TypeScript Works

TypeScript is a compiled language. The TypeScript compiler (tsc) reads .ts files and emits plain .js files:

your-code.ts  →  tsc  →  your-code.js

The types are erased during compilation — they exist only at development time and have zero runtime overhead.


Getting Started

Install TypeScript

// Install globally
// npm install -g typescript

// Or as a project dependency (recommended)
// npm install --save-dev typescript

Initialise a Project

// npx tsc --init
// This creates a tsconfig.json with sensible defaults

The TypeScript Playground

The official TypeScript Playground lets you experiment with TypeScript directly in the browser. It is an excellent tool for:

  • Trying out new features
  • Sharing code snippets with colleagues
  • Viewing the compiled JavaScript output side-by-side

TypeScript vs JavaScript

Feature TypeScript JavaScript
Type checking Static (compile-time) Dynamic (runtime)
Interfaces Yes No
Generics Yes No
Enums Yes No (only via workarounds)
IDE support Excellent Good
Learning curve Moderate Low
Adoption Growing rapidly Universal

TypeScript is not a replacement for JavaScript — it is JavaScript with guardrails. Every valid JavaScript programme is also valid TypeScript, which makes migration straightforward.