TypeScript is a open-source, strongly typed programming language developed by Microsoft that builds directly on JavaScript by adding static type definitions. Every valid JavaScript program is valid TypeScript, but TypeScript provides a compile-time safety layer that validates variable types, function parameters, object structures, and API payloads before code ever runs in production.
JavaScript is dynamically typed, meaning variables can change types at runtime without warning. In complex web applications or microservices, dynamic typing frequently leads to runtime bugs such as TypeError: Cannot read properties of undefined or invalid data mutations.
TypeScript solves these problems by providing:
| Feature | JavaScript (JS) | TypeScript (TS) |
|---|---|---|
| Type System | Dynamic typing (evaluated at runtime) | Static typing (validated at compile-time) |
| Execution | Interpreted directly by browsers/Node.js | Transpiled (tsc) into standard JavaScript |
| Type Annotations | Not supported natively | Supported (string, number, interfaces, generics) |
| Tooling & IDE | Basic autocompletion, prone to hidden errors | Instant type checking, jump to definition, safe refactoring |
| Interfaces & Enums | Not available | Built-in first-class language constructs |
TypeScript source files (.ts) do not run directly inside browser engines or standard Node.js runtimes. Instead, the TypeScript compiler (tsc) parses the code, enforces type constraints, and strips out all type annotations to generate plain JavaScript (.js).
flowchart LR
A["TypeScript Source (.ts)"] --> B["tsc Compiler (Type Checking)"]
B -- "Type Errors Detected" --> C["Build Fails / Warnings"]
B -- "Types Validated" --> D["Clean JavaScript (.js)"]
D --> E["Browser V8 / Node.js Engine"]
Here is a practical comparison showing how TypeScript adds safety to a user notification service:
// Interface contract defining strict payload requirements
interface User {
id: number;
name: string;
email: string;
role: "admin" | "member" | "guest";
isActive: boolean;
}
function sendWelcomeNotification(user: User): string {
if (!user.isActive) {
return `User ${user.name} (ID: ${user.id}) is inactive. Notification skipped.`;
}
return `Sending welcome email to ${user.name} at <${user.email}> [Role: ${user.role.toUpperCase()}]`;
}
// Valid user object matching contract
const newUser: User = {
id: 101,
name: "Sarah Connor",
email: "[email protected]",
role: "admin",
isActive: true
};
console.log(sendWelcomeNotification(newUser));
"strict": true in tsconfig.json to enable strict null checks, implicit any warnings, and strict function types.any: Suppressing type checking with any defeats the core purpose of TypeScript. Use unknown or explicit generics instead.Define a TypeScript interface named Product with fields id (number), title (string), and price (number). Write a function applyDiscount(product: Product, percent: number): number that returns the discounted price.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With