Skip to content
Tech Interview Prep
Technical interview guide

TypeScript for Frontend Engineers

TypeScript's type system as frontend code actually uses it: structural typing, generics, narrowing, utility types, and typing React props, hooks and events. Plus what the compiler catches at build time, what it erases, and where runtime validation is still required.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v2
Editorial status
Reviewed

Scope: TypeScript 5.x (checked against 5.8), React 18 and 19 with @types/react 18/19. `satisfies` requires TS 4.9+; inferred type predicates require 5.5+; `verbatimModuleSyntax` requires 5.0+. Defaults quoted are from the TS 5.8 `tsc --init` output..

Overview

Curated: · Written: · Reviewed:

TypeScript for Frontend Engineers

In one line

TypeScript is a build-time shape checker: it proves what the compiler can see and erases everything else. Reach for it to make illegal states unrepresentable in components, hooks and API types, and add runtime validation wherever data enters from outside.

Key definitions

  • Structural typing: assignability by shape. Extra members are fine, missing ones fail.
  • Excess property check: applies to object literals only, not to variables that already hold the extra keys.
  • Discriminated union: members share a literal field, such as kind, that narrows the rest of the shape.
  • Type predicate: x is T. The compiler trusts the function's result and never verifies its body.
  • Declaration merging: same-named interfaces combine. Type aliases do not.
  • Exhaustiveness: passing the leftover case to a never parameter turns a missing case into a compile error.

How it works

  1. tsc checks the whole program, then erases every annotation, interface and generic. The runtime runs plain JavaScript.
  2. The compiler tracks each binding through its branches. Each typeof, in, instanceof or discriminant check removes possibilities.
  3. as casts and predicates skip the check or trust it. Neither emits code, so a wrong one fails only at runtime.
  4. Generics infer type arguments from call-site values. A constraint such as T extends { id: string } rejects callers whose values lack the member.
Source ts and tsx tsc type check Types erased JavaScript runs fetch, localStorage, URL params Typed as unknown Schema parse Typed value in app code

When to use and when not to

ChoiceReach for it whenAvoid it when
Discriminated unionstate has mutually exclusive shapesone optional field describes the state
type aliasunions, mapped and derived typesyou need declaration merging
interfacea public object shape consumers may augmentyou need a union or mapped type
Generic parameterthe caller's value decides the typeone concrete type is correct
Type predicatea real runtime check backs itthe body is a guess
Schema parsedata crosses a network, storage or URL boundarydata is already typed internally
enumalmost never; prefer string unions or as const objectsunder isolatedModules, since const enum is problematic

Traps interviewers probe

  • Treating a response annotation as validation. r.json() is Promise<any> and as User only records a claim. Fix: type the body as unknown, parse it with a schema inside the hook, and throw there, before any component renders.
  • Assuming excess property checks always fire. const opts = { retries: 3, timeout: 30_000 }; f(opts) passes because the variable is not a fresh literal. Fix: pass the literal, or make the function read every key it accepts, so a dropped timeout cannot hide.
  • Freezing state or refs to the wrong type. useState('idle' as const) locks the type to 'idle'. Fix: useState<Status>('idle'). useRef<HTMLDivElement>(null) is nullable on purpose; fix the call with ref.current?.scrollIntoView(), not a non-null assertion.
  • Extracting a handler without annotating it. function h(e) {} is implicit any. Fix: annotate the event with the element it is attached to, such as React.ChangeEvent<HTMLInputElement> or React.MouseEvent<HTMLButtonElement>.
  • Silencing a never error. It means the reducer lacks a case. Fix: add the case. Do not cast, and do not delete the exhaustive default.

Interview tips

Open with the model: types are erased, so they prove shape at build time and prove nothing at runtime.

Follow-ups to expect:

  • What TypeScript does not catch: network data, any leaks, lying casts, runtime invariants.
  • type versus interface: merging, stricter extends overrides, unions only with type.
  • satisfies keeps literal types and still checks the shape. An annotation widens them. as const keeps literals but checks nothing.
  • Generic arrow components in .tsx need <T,> or <T extends unknown>, because a bare <T> parses as a JSX tag.
  • Context: createContext<T | null>(null) with a hook that throws when the provider is missing.
  • Variance: method syntax is bivariant, while function-typed props are checked contravariantly under strictFunctionTypes.
  • Derive, do not redeclare: Omit, Pick, Awaited<ReturnType<typeof fn>>, and key remapping with template literals.
  • Module boundaries: type-only re-exports need export type under isolatedModules or verbatimModuleSyntax. swc and esbuild strip types, so tsc --noEmit runs as its own CI job.
  • Untyped dependencies: @types/* first, then a hand-written declare module file. Module augmentation works only on interfaces.

30-second outline:

  1. Definition: TypeScript checks shape before the program runs and erases every type.
  2. Mechanism: narrowing through discriminants and checks; never catches missing cases.
  3. Limit: nothing checks runtime data, so validate at each I/O boundary.
  4. Example: a discriminated union for request state, with a never default, and a schema parse in the fetch hook.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.