Tech Interview RoomStudy guides

TypeScript

What is TypeScript and why do teams use it for frontend code?

Beginner · 2 min read · Lesson 1 of 7 in TypeScript

TypeScript is JavaScript plus a static type system. The compiler (tsc) or a faster transpiler (esbuild, SWC, Babel) removes the types and emits plain JavaScript, so types have no runtime cost and no runtime checks.

Benefits

  • Catches whole classes of bugs before running: typos in property names, undefined access, wrong argument types.
  • Editor support: autocomplete, go-to-definition, safe rename across the codebase.
  • Types document component props, API responses and store shapes.
  • Refactoring large React or Vue apps becomes much safer.

Things to know

  • Turn on "strict": true (includes strictNullChecks and noImplicitAny). Without it most of the value is lost.
  • Types are erased, so data from the network is not validated. Use a schema library such as Zod or Valibot at the boundary.
  • Vite and most bundlers only strip types; run tsc --noEmit (or vue-tsc for Vue) in CI to actually type-check.

Example

ts
interface User {
  id: number;
  name: string;
  email?: string;            // optional
}

function greet(user: User): string {
  return `Hello ${user.name.toUpperCase()}`;
}

greet({ id: 1, name: 'Asha' });
// greet({ id: 1 });          // error: property 'name' is missing
// user.email.length          // error: 'email' is possibly undefined

Follow-up questions interviewers ask

Try answering each one before you open it.

What is the difference between any and unknown?

any switches type checking off: you can do anything with it and errors spread silently. unknown means "some value I have not checked yet": you must narrow it (typeof, instanceof, a type guard or a schema parse) before using it. Use unknown for catch (e) variables, JSON.parse results and untrusted input.