Difficulty: Beginner
Why would a team choose TypeScript over plain JavaScript? What problems does TypeScript actually solve, and what does it not solve?
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.
order.amout or passing a string where a number is expected is an error in the editor, not a production incident.strictNullChecks, user.address.city fails to compile if address can be missing.Order type and the compiler lists every file that breaks. This is the biggest win in large codebases with many contributors.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.
| Cost | How teams handle it |
|---|---|
| Build step | tsc, 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 types | Most app code needs only basic types, unions and generics |
| Typing untyped libraries | @types/* packages from DefinitelyTyped, or a local .d.ts |
Escape hatches like any | Lint rules such as no-explicit-any and strict mode |
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.
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.
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.
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.
// @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.
Static Typing, Type Erasure, Compile-Time Checks, Developer Tooling, Gradual Typing