Why TypeScript? JS vs TS

Difficulty: Beginner

Question

Why would a team choose TypeScript over plain JavaScript? What problems does TypeScript actually solve, and what does it not solve?

Answer

TypeScript is JavaScript plus a static type system that is checked at compile time and then erased. It catches a large class of bugs before code runs (typos, wrong argument shapes, missing null checks, broken refactors) and gives editors enough information for accurate autocomplete, go-to-definition and safe renames. It does not change runtime behaviour or validate data coming from outside your program.

Problems TypeScript solves

What it does not solve

TypeScript types vanish after compilation. If an API returns { amount: "499" } instead of a number, your Order type will not stop it. You still need runtime validation (Zod, Valibot, JSON Schema) at boundaries: HTTP bodies, environment variables, local storage, third-party webhooks. It also cannot prove business logic correct; tests are still required.

The costs

CostHow teams handle it
Build steptsc, esbuild, SWC or Vite strip types quickly; Node.js 22.18+ and 23.6+ can run .ts files directly by stripping erasable types
Learning curve for advanced typesMost app code needs only basic types, unions and generics
Typing untyped libraries@types/* packages from DefinitelyTyped, or a local .d.ts
Escape hatches like anyLint rules such as no-explicit-any and strict mode

How it fits the ecosystem in 2026

TypeScript is the default for new React, Next.js, Angular, NestJS and Node projects, and most popular npm libraries ship their own types. The compiler is also being ported to Go (the TypeScript 7 native preview, tsgo) for much faster type checking on large projects, while the language itself stays the same. Because it is a superset of JavaScript, adoption can be gradual: rename files one by one, start with allowJs, and tighten strictness over time.

When plain JavaScript is fine

Small scripts, quick prototypes, or a codebase where a JSDoc // @ts-check comment gives enough checking without a build step. Even then, many teams use JSDoc types checked by the TypeScript compiler, so they get most of the benefit.

Where TypeScript does its work. Types are checked during development and build, then removed before runtime.
  1. Write .ts with types: Editor shows errors as you type.
  2. Type check with tsc: Catches mismatches and null bugs.
  3. Strip types to .js: tsc, esbuild, SWC or Node type stripping.
  4. Run plain JavaScript: No types exist at runtime.

Code examples

Bugs caught before runtime

interface Order {
  id: string;
  amountPaise: number;
  coupon?: string;
}

function totalRupees(orders: Order[]): number {
  return orders.reduce((sum, o) => sum + o.amountPaise, 0) / 100;
}

totalRupees([{ id: "1", amountPaise: "49900" }]);
// Error: Type 'string' is not assignable to type 'number'.

function couponCode(o: Order) {
  return o.coupon.toUpperCase();
  // Error: 'o.coupon' is possibly 'undefined'. (strictNullChecks)
}

Two classic JavaScript production bugs, a stringly-typed amount and a missing optional field, become compile errors.

Types do not validate runtime data

import { z } from "zod";

const OrderSchema = z.object({
  id: z.string(),
  amountPaise: z.number().int().nonnegative(),
});
type Order = z.infer<typeof OrderSchema>; // type derived from schema

async function loadOrder(id: string): Promise<Order> {
  const res = await fetch(`/api/orders/${id}`);
  // Without parse, a wrong payload would still be typed as Order
  return OrderSchema.parse(await res.json());
}

The compiler trusts annotations on external data. A runtime schema checks the payload and also produces the static type, so the two cannot drift apart.

Type checking plain JavaScript with JSDoc

// @ts-check

/**
 * @param {number} amountPaise
 * @returns {string}
 */
function formatInr(amountPaise) {
  return new Intl.NumberFormat("en-IN", {
    style: "currency",
    currency: "INR",
  }).format(amountPaise / 100);
}

formatInr("499"); // Error reported by the TS language service

The @ts-check comment makes the TypeScript checker analyse a .js file using JSDoc types, a common first step in gradual adoption.

Key points

Common follow-up questions

Interview tips

Concepts covered

Static Typing, Type Erasure, Compile-Time Checks, Developer Tooling, Gradual Typing

More TypeScript Interview questions