What is TypeScript and Why Use It?
Learn what TypeScript is, how static typing catches bugs at compile time, and why teams use it to write safer, more maintainable JavaScript at scale.
Expected Interview Answer
TypeScript is a strongly typed superset of JavaScript that adds static type checking and compiles down to plain JavaScript that runs anywhere JavaScript runs.
You annotate variables, function parameters, and return values with types, and the TypeScript compiler (tsc) checks them before the code ever runs, catching a whole class of bugs at build time. Because it is a superset, any valid JavaScript is already valid TypeScript, so teams can adopt it incrementally. The type information also powers rich editor features like autocomplete, safe refactoring, and inline documentation.
- Catches type errors at compile time, before runtime
- Better editor autocomplete and IntelliSense
- Safer, tool-assisted refactoring on large codebases
- Types act as always-accurate documentation
- Incremental adoption on existing JavaScript projects
AI Mentor Explanation
TypeScript is like a third umpire reviewing every delivery before it counts: the on-field play is your JavaScript, but the review checks the ball was legal — no-ball, wide, or wrong type — and flags mistakes before the run is added to the scoreboard rather than after the match is lost.
Step-by-Step Explanation
Step 1
Install TypeScript
Add the compiler with npm install -D typescript and create a tsconfig.json with npx tsc --init.
Step 2
Write typed code
Give a .ts file, annotating variables, parameters, and return values with types where inference is not enough.
Step 3
Compile
Run npx tsc so the compiler type-checks the code and emits plain JavaScript.
Step 4
Fix reported errors
Resolve any type errors the compiler surfaces before the code ships or runs.
Step 5
Run the output
Execute the emitted .js in Node or the browser exactly like ordinary JavaScript.
What Interviewer Expects
- Knows TypeScript is a typed superset of JavaScript
- Understands static type checking happens at compile time
- Can explain that it compiles to plain JavaScript
- Mentions tooling benefits like autocomplete and refactoring
- Aware that adoption can be incremental
Common Mistakes
- Thinking TypeScript runs directly in the browser without compilation
- Believing types exist at runtime (they are erased)
- Saying TypeScript replaces JavaScript rather than compiling to it
- Confusing type annotations with runtime validation
Best Answer (HR Friendly)
“TypeScript is a version of JavaScript that lets you label what kind of data each part of your code expects. It checks those labels while you write, so many mistakes are caught early, and it then turns into normal JavaScript that runs everywhere.”
Code Example
// TypeScript catches the bug before it runs
function add(a: number, b: number): number {
return a + b;
}
add(2, 3); // 5
add(2, '3'); // Error: Argument of type 'string' is not assignable to 'number'
// Compiles to plain JavaScript:
// function add(a, b) { return a + b; }Follow-up Questions
- How does TypeScript's type erasure affect the runtime?
- What is the difference between a superset and a new language?
- How would you incrementally adopt TypeScript in a JavaScript project?
- What does the tsconfig.json strict flag change?
- What are the trade-offs of adding TypeScript to a small project?
MCQ Practice
1. What best describes TypeScript?
TypeScript is a superset of JavaScript that adds static types and compiles down to plain JavaScript.
2. When does TypeScript's type checking primarily occur?
The TypeScript compiler checks types at build time, catching errors before the JavaScript is executed.
3. What happens to type annotations after compilation?
Types are erased during compilation; the emitted JavaScript contains no type information.
Flash Cards
What is TypeScript? — A statically typed superset of JavaScript that compiles to plain JavaScript.
When are TypeScript types checked? — At compile time by the tsc compiler, before the code runs.
Do TypeScript types exist at runtime? — No — they are erased during compilation and do not affect runtime behavior.
Can you adopt TypeScript incrementally? — Yes — any valid JavaScript is valid TypeScript, so it can be added file by file.