100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
TypeScript

TypeScript vs JavaScript Interview Questions

Contrastive interview questions on why and how TypeScript extends JavaScript, covering compile-time vs runtime checking, structural typing, JS interop, and migration strategy.

Interview PrepIntermediate15 min readJul 8, 2026
Analogies

1. Overview

Interviewers frequently probe whether candidates understand not just TypeScript syntax but why it exists relative to plain JavaScript, and how the two interoperate in real projects. This topic focuses specifically on contrastive questions: what TypeScript adds on top of JavaScript, the difference between compile-time and runtime type checking, structural versus nominal typing, how TypeScript consumes existing JavaScript code and libraries, and practical strategies for migrating a JavaScript codebase to TypeScript incrementally. Use it to prepare for the 'why TypeScript' portion of an interview, which is common even in roles that are primarily JavaScript-focused.

🏏

Cricket analogy: This topic is like comparing a fully professional league with certified umpires and DRS to gully cricket with no formal rules -- examining what structure TypeScript adds, when checks happen, and how to gradually formalize a casual team into a certified one.

2. Frequently Asked Questions

Q1. Why would a team choose TypeScript over plain JavaScript?

TypeScript catches an entire class of bugs — wrong argument types, typos in property names, null/undefined access, mismatched function signatures — at compile time instead of in production. It also improves editor tooling significantly: accurate autocomplete, inline documentation, safe rename-refactoring, and jump-to-definition all rely on static types. On larger teams and codebases, types serve as executable documentation of function contracts and data shapes, reducing the cognitive load of understanding unfamiliar code and making large-scale refactors far safer.

🏏

Cricket analogy: TypeScript catching a wrong argument type at compile time is like a coach spotting a batter's flawed grip before the match starts, rather than watching them get bowled out live; type annotations also work like a detailed scouting report, letting a new selector instantly understand a player's role.

Q2. What exactly does TypeScript add on top of JavaScript at the language level?

TypeScript is a strict syntactic superset: it adds optional static type annotations, interfaces, generics, enums, type aliases, access modifiers on class members (public/private/protected), abstract classes, and advanced type-level features (mapped types, conditional types, template literal types). It does not add new runtime behavior or new JavaScript language features by itself beyond what the configured compilation target already supports — all of TypeScript's own additions are erased during compilation, leaving plain JavaScript.

🏏

Cricket analogy: TypeScript is a strict superset the way a professional coaching manual adds annotated techniques, drills, and terminology on top of the same underlying game of cricket -- none of these coaching notes change the actual physics of how a ball is bowled or hit, they're erased once the player just plays.

Q3. What is the difference between compile-time type checking and runtime type checking?

Compile-time type checking happens when the TypeScript compiler (or an editor's language service) analyzes your source code before it ever runs, flagging type mismatches as errors during development or the build step. Runtime type checking happens while the program executes, using JavaScript's native mechanisms (typeof, instanceof, Array.isArray) or a validation library (zod, io-ts) to inspect actual values. Crucially, TypeScript's types are entirely erased at compile time and provide zero protection at runtime — data coming from an API response, user input, or JSON.parse still needs runtime validation if you want real safety, because TypeScript cannot verify that external data actually matches the declared type.

🏏

Cricket analogy: A pre-match pitch inspection is compile-time checking -- flagging problems before play begins -- while an umpire's live no-ball call is runtime checking, happening as the delivery is bowled; but TypeScript's own checks vanish once the real match starts, so an unpredictable data source like a fan scorecard still needs a real, live check.

Q4. Does adding type annotations change how JavaScript code behaves once compiled?

No. Type annotations, interfaces, and type-only constructs are fully erased during compilation and have no effect on the emitted JavaScript's runtime behavior — a correctly typed program and an equivalent untyped program compile to functionally identical JavaScript (aside from features like enums and the legacy 'parameter properties' shorthand in constructors, which do generate real runtime code). Types exist purely to catch mistakes during development; at runtime, TypeScript's checks simply do not exist anymore.

🏏

Cricket analogy: A batting order's formal designation ("No. 3", "No. 4") is fully erased once the innings actually starts -- the players just bat in whatever order they walk out, aside from a few rules like the powerplay that do have real, enforced runtime effect, unlike a mere batting-order label.

Q5. What is structural typing, and how does it differ from the nominal typing used in languages like Java?

TypeScript uses structural typing: two types are considered compatible if they have the same shape (matching members and types), regardless of how or where they were declared. In nominal typing (Java, C#), a class must explicitly declare 'implements SomeInterface' to be considered compatible with that interface, even if its shape matches exactly. This means in TypeScript, an object literal or class instance can satisfy an interface it was never explicitly declared to implement, which is more flexible but can occasionally allow unintended compatibility between unrelated types that merely share a shape.

🏏

Cricket analogy: TypeScript's structural typing is like a scout who cares only whether a player can actually bat, bowl, and field like an all-rounder -- regardless of whether their contract literally says "all-rounder"; nominal typing (Java/C#) requires the contract to explicitly state the role first, even if the skills match perfectly.

Q6. Since JavaScript has no static types, how does TypeScript type-check calls into existing JavaScript libraries?

TypeScript relies on separate '.d.ts' declaration files that describe a library's shape (functions, classes, exported values) without containing any implementation. Many popular libraries ship their own .d.ts files; for others, the community-maintained DefinitelyTyped project provides @types/ packages (e.g. npm install --save-dev @types/lodash) that can be installed alongside a plain JS library. If no declarations exist at all, TypeScript treats the imported values as 'any' by default (or errors under certain settings), and a developer can write custom ambient declarations to add type safety incrementally.

🏏

Cricket analogy: A .d.ts file describing a bowling-machine library's shape without its actual mechanism is like a spec sheet listing "speed, spin, line" settings without revealing the internal gears; if no official spec exists, the community publishes an unofficial one, or you write your own, or you operate it blind.

Q7. How can a plain JavaScript file gradually adopt type checking without being renamed to .ts?

With 'checkJs': true (and 'allowJs': true) in tsconfig.json, the TypeScript compiler will type-check .js files in place using inferred types and JSDoc comments, without requiring a rename to .ts. Developers can add JSDoc annotations like '/** @param {string} name */' to get real type checking and editor autocomplete on plain JavaScript functions. This is a common intermediate step before a full migration, letting teams get typing benefits file by file without committing to TypeScript syntax everywhere at once.

🏏

Cricket analogy: checkJs: true type-checking a .js file in place using inference and JSDoc comments is like a coach reviewing a veteran player's raw technique on video without making them formally re-enroll in the academy -- real, actionable feedback without the full retraining program.

A common approach: (1) add a tsconfig.json with 'allowJs': true and 'checkJs': false initially so both .js and .ts files can coexist and compile together; (2) rename files to .ts/.tsx incrementally, starting with leaf modules that have few dependencies, fixing type errors as they surface; (3) start with loose settings (noImplicitAny: false) to avoid being overwhelmed, then progressively tighten to full 'strict': true as coverage grows; (4) add @types packages for third-party dependencies; (5) prioritize converting shared utility/data-model files early since their types propagate benefits throughout the codebase. This avoids a risky 'big bang' rewrite.

🏏

Cricket analogy: A gradual migration strategy -- starting loose, converting simple fringe players (leaf modules) first, then tightening standards to full professionalism, prioritizing the core batting order early since its standards ripple through the whole team -- beats a risky "big bang" overhaul overnight.

Q9. If TypeScript's checks are erased at compile time, why doesn't a wrong type ever crash a compiled TypeScript program directly due to a type error?

Because by the time code is compiled, all type errors should already have been caught and fixed — 'tsc' refuses (by default) to consider a build fully clean if there are type errors, though it can still emit output alongside those errors unless noEmitOnError is set. Once compiled, there is no type information left to check, so any type-related bug that slipped past compile time (for example due to an 'any' type, a type assertion using 'as', or invalid external data) will simply manifest as a normal JavaScript runtime error, like calling a method on undefined, with no TypeScript-specific runtime guard involved.

🏏

Cricket analogy: tsc refusing to call a build fully "clean" while type errors remain is like an umpire refusing to certify a pitch as match-ready while cracks are visible, though play might still proceed under protest; once the match actually starts, any missed crack just causes a bad bounce live, with no umpire flag to catch it.

Q10. How does TypeScript handle a JavaScript function that has no JSDoc types and is called with mismatched arguments?

If the function lives in a .js file without 'checkJs' enabled, TypeScript does not check it at all — parameters are effectively 'any' and no error is raised regardless of what arguments are passed. If 'checkJs' is enabled but no JSDoc annotations exist, TypeScript still infers parameter types where possible (e.g., from default values or usage) but otherwise treats untyped parameters as implicit 'any', meaning many mismatches will still go undetected until JSDoc types or a .ts rewrite are added.

🏏

Cricket analogy: A bowler's run-up with no formal coaching video review (no checkJs) means any flaw goes completely unnoticed, params effectively "anything goes"; with review enabled but no detailed notes (no JSDoc), a coach still catches obvious flaws by eye, but subtler ones ("implicit any") still slip through.

Q11. Are there any TypeScript-only runtime features, or is everything purely compile-time?

Almost everything in TypeScript is purely a compile-time construct that gets erased, but a few features do generate actual runtime JavaScript code: numeric and string enums compile into real objects; the legacy class 'parameter properties' shorthand (constructor(private x: number)) generates real assignment code; and namespaces compile into IIFE-wrapped objects. Interfaces, type aliases, generics, and type annotations, by contrast, are 100% erased and produce zero runtime footprint.

🏏

Cricket analogy: Most coaching terminology is purely conceptual and leaves no trace on the field, but a few things -- like the actual mandatory helmet rule -- generate real, enforced behavior at match time; a player's informally noted "role" (interface) leaves zero trace, while an official league regulation (enum-like) actually changes what happens on the pitch.

Q12. Can TypeScript catch a bug where an API returns JSON with a different shape than the declared type?

No, not on its own. const data = await response.json() as User; uses a type assertion, which tells the compiler 'trust me, treat this as a User' without performing any real verification — if the API actually returns a different shape, TypeScript will not catch the mismatch, and the bug will only surface as a runtime error (e.g., undefined.someMethod()) when the code accesses a property that isn't really there. To genuinely guard against this, you need runtime validation (a library like zod, or manual property checks) in addition to the TypeScript type.

🏏

Cricket analogy: Asserting a scraped web page's data as PlayerStats doesn't verify the page actually has those fields -- if the site's layout changed, the code compiles cleanly, and the bug only surfaces when the app crashes trying to read a "strikeRate" field that no longer exists on the real page.

3. Quick Reference

  • TypeScript = JavaScript + compile-time static types; types are fully erased before runtime.
  • TypeScript types provide zero protection against untrusted runtime data (API responses, JSON.parse, user input).
  • TypeScript uses structural typing; Java/C# use nominal typing requiring explicit 'implements'.
  • .d.ts declaration files (own or @types/ from DefinitelyTyped) type existing JS libraries.
  • allowJs + checkJs enables type-checking plain .js files via inference and JSDoc, no rename required.
  • Enums, parameter properties, and namespaces are the rare TypeScript features with real runtime output.

4. Key Takeaways

  • TypeScript adds compile-time safety and tooling on top of JavaScript without changing runtime semantics for the vast majority of features.
  • Compile-time type checks are erased before execution, so external/untrusted data still needs runtime validation.
  • Structural typing makes TypeScript more flexible than nominally typed languages but can allow accidental type compatibility.
  • .d.ts files and @types packages are how TypeScript understands plain JavaScript libraries.
  • Migration from JS to TS is typically incremental: allowJs/checkJs, then per-file renames, then progressively strict settings.

Practice what you learned

Was this page helpful?

Topics covered

#TypeScript#TypeScriptProgrammingStudyNotes#Programming#TypeScriptVsJavaScriptInterviewQuestions#JavaScript#Interview#Questions#Frequently#StudyNotes#SkillVeris