# Why TypeScript? JS vs TS

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

Canonical: https://www.internhack.in/learn/interview/typescript-interview/ts-why-typescript
Last updated: 2026-11-28

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

- **Shape mismatches**: calling `order.amout` or passing a string where a number is expected is an error in the editor, not a production incident.
- **Null and undefined bugs**: with `strictNullChecks`, `user.address.city` fails to compile if `address` can be missing.
- **Refactoring at scale**: rename a field in a shared `Order` type and the compiler lists every file that breaks. This is the biggest win in large codebases with many contributors.
- **Documentation that cannot go stale**: types describe what a function accepts and returns, and the compiler keeps them honest.
- **Tooling**: VS Code's IntelliSense for plain JavaScript is itself powered by the TypeScript language service.

### 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

| 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 |

### 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

- TypeScript is a superset of JavaScript; types are erased and add no runtime cost.
- Biggest payoffs are safe refactoring, null safety and editor tooling.
- Types do not validate external data; use a runtime schema at boundaries.
- Node.js 22.18+ and 23.6+ can run TypeScript files directly by stripping types, but still do not type check.
- Adoption can be incremental with allowJs, checkJs and JSDoc.

## Common follow-up questions

- If types are erased, how do you validate API responses at runtime?
- What does strict mode enable, and would you turn it on for an existing project?
- How would you convince a team to migrate a JavaScript codebase?
- What is the difference between type checking and transpiling?

## Interview tips

- Give a concrete bug story, such as a renamed field breaking a checkout page, rather than generic "type safety" claims.
- Always mention that types are erased; it shows you know the limits.
- Acknowledge the costs honestly and explain how gradual adoption reduces them.

## Concepts covered

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

Previous: [Type Guards & Narrowing](https://www.internhack.in/learn/interview/typescript-interview/ts-type-guards-narrowing) · Next: [Basic Types & Annotations](https://www.internhack.in/learn/interview/typescript-interview/ts-basic-types) · [All of TypeScript Interview](https://www.internhack.in/learn/interview/typescript-interview)

## More TypeScript Interview questions

- [Arrays, Tuples & Enums](https://www.internhack.in/learn/interview/typescript-interview/ts-arrays-tuples-enums)
- [Functions & Overloads](https://www.internhack.in/learn/interview/typescript-interview/ts-functions-overloads)
- [Object Types & Optional Props](https://www.internhack.in/learn/interview/typescript-interview/ts-object-types)
- [Union & Intersection Types](https://www.internhack.in/learn/interview/typescript-interview/ts-union-intersection)
- [Literal Types & const Assertions](https://www.internhack.in/learn/interview/typescript-interview/ts-literal-const)
- [unknown vs any vs never](https://www.internhack.in/learn/interview/typescript-interview/ts-unknown-any-never)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [TypeScript Interview](https://www.internhack.in/learn/interview/typescript-interview)
