What is TypeScript, and how is it different from JavaScript? What happens to the types when the code runs?
Show the explanation
Model answer. TypeScript is JavaScript plus a static type system. You annotate (or let it infer) the shapes of values, and the compiler reports mistakes such as calling a missing method, passing a string where a number is expected, or forgetting a null check, before the code runs.
type Student = { name: string; marks: number[] };
function average(s: Student): number {
return s.marks.reduce((a, b) => a + b, 0) / s.marks.length;
}
average({ name: 'Asha', marks: '90' }); // compile error: string is not number[]Key facts:
- Types are erased when compiled: the output is plain JavaScript, and nothing is checked at runtime. Data from an API still needs runtime validation.
- Every valid JavaScript file is (mostly) valid TypeScript, so projects can migrate gradually.
- Benefits: earlier bug detection, better editor autocomplete and refactoring, and types that document intent in large codebases.
- Cost: a build step and some learning; overly clever types can hurt readability.
Original UniCircle question. Last reviewed 22 Oct 2026.
