Skip to the solution
EasyFrontend10 min read

What is TypeScript and why use it over JavaScript?

666 views

The problem

Question presented to candidate: "Your team is starting a new project and someone asks why we would use TypeScript instead of plain JavaScript. What is TypeScript, what does it actually give us, and what does it cost?"

What a strong answer should cover:

  • TypeScript is JavaScript plus a static type system: tsc checks types before the code runs, then erases them and emits plain JavaScript
  • Concrete bugs it catches that JavaScript runs anyway: property typos (NaN), wrong arguments, missing fields, possibly-undefined values
  • Type erasure: no run-time cost and no run-time checking, so data at trust boundaries (JSON, network) still needs validation
  • Tooling and refactoring benefits: autocomplete, safe renames, signatures as documentation; structural typing and gradual adoption
  • Honest costs: a build or check step (Node.js type stripping does not type-check), learning curve, false confidence from any and as, and the fact that plain tsc still emits on errors unless noEmitOnError is set

Code / implementation expected: Optional β€” a small typed function showing an error JavaScript would only reveal at run time.

  • typescript
  • basics

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

10 min read Β· 10 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptThe bugs TypeScript catches that JavaScript runs anyway
TypeScriptTypes are erased: only a run-time check protects you

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 7 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What are tuple types?

Continue