[Easy] TypeScript vs JavaScript
1. What is TypeScript?
How is TypeScript related to JavaScript?
TypeScript is a superset of JavaScript that adds static typing and developer tooling.
- All valid JavaScript is valid TypeScript
- TypeScript code is compiled to JavaScript before running
- Type errors are found during development, not only at runtime
2. TypeScript vs JavaScript at a glance
| Topic | JavaScript | TypeScript |
|---|---|---|
| Type system | Dynamic | Static + inferred |
| Compile step | Not required | Required (tsc or bundler) |
| Error detection | Mostly runtime | Compile time + runtime |
| Refactoring support | Basic | Strong |
| Learning curve | Lower | Higher |
3. Example comparison
JavaScript
function add(a, b) {
return a + b;
}
console.log(add(1, '2')); // "12" (maybe unintended)
TypeScript
function add(a: number, b: number): number {
return a + b;
}
// add(1, '2'); // compile-time error
console.log(add(1, 2));
TypeScript helps prevent accidental coercion bugs.
4. Compilation flow
- Write
.tsor.tsx - Type check with TypeScript compiler
- Emit JavaScript
- Run JavaScript in browser or Node.js
You still ship JavaScript to production.