Skip to content
Tech Interview Prep
Technical interview guideNew

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.

Read
18 min
Practice MCQs
25
Interview Q&A
25
Edition
v1
Editorial status
Reviewed
Relevant for
Frontend Engineer

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..

Curated: · Written: · Reviewed:

25 questions

MCQ-10A controlled input ships as `onChange={(e) => setName(e.target.value)}` and compiles. A reviewer asks for the arrow hoisted into a named function in the same file: `function handleNameChange(e) { setName(e.target.value); }`, passed as `onChange={handleNameChange}`. With `strict` on, `tsc` now reports `Parameter 'e' implicitly has an 'any' type`. Which annotation keeps `e.target.value` a `string`?
MCQ-8A component keeps its request status in state and handles exactly `'idle' | 'loading' | 'error'`. A teammate writes `const [status, setStatus] = useState('idle' as const)`, and `tsc --strict` rejects the next line, `setStatus('loading')`, with `Argument of type '"loading"' is not assignable to parameter of type '"idle"'`. Which change fixes that call and leaves `status` typed as the three-state union?
MCQ-15A feature-flag module declares `const flags = { checkoutV2: 'on', newNav: 'off' }` and exposes `setFlag(name: keyof typeof flags, value: string)`. The team wants `setFlag('checkoutV3', 'on')` to fail the build, and a typo such as `checkoutV2: true` in the object to fail it too. Which declaration of `flags` gives both?
MCQ-16A `NavLink` resolves its `to` from a `name` prop typed `string` (it comes from a CMS payload). Routes are declared once as `const ROUTES = { home: '/', settings: '/settings', profile: '/profile' } as const;`, and `function hrefFor(name: string) { return ROUTES[name]; }` fails `tsc --strict` with TS7053. Which change compiles, keeps the return type as `'/' | '/settings' | '/profile'` for a valid name, and rejects an unknown name at runtime?
MCQ-1A `Badge` component's props are `{ kind: 'text'; value: string } | { kind: 'number'; value: number }`. In the text branch it must call `formatText(props)`, declared as `function formatText(p: { kind: 'text'; value: string }): string`. Which condition makes `tsc --strict` accept that call with no cast?
MCQ-5A cart reducer's `switch (action.type)` ends with `default: { const _exhaustive: never = action; return state; }`, where `Action` is a discriminated union of `{ type: 'add'; sku: string }` and `{ type: 'remove'; sku: string }`. A PR adds `| { type: 'clear' }` to `Action` without touching the reducer, and `tsc --noEmit` now reports `Type '{ type: "clear"; }' is not assignable to type 'never'` on that default line. What is the failure telling the team, and what response is right?
MCQ-3A team's `fetchWithRetry(opts: { retries: number })` reads only `opts.retries`. A caller writes `fetchWithRetry({ retries: 3, timeout: 30_000 })` and `tsc` rejects it with an excess property error; the author instead writes `const opts = { retries: 3, timeout: 30_000 }; fetchWithRetry(opts);`, which compiles, and the PR ships. Each attempt still gives up at 5 s. Which account of the two call forms is right, and what should the reviewer ask for?
MCQ-24A design-system `IconButton` declares `type IconButtonProps = { label: string; onPress: (e: MouseEvent) => void }` and forwards the click event it receives to `onPress`. A page passes `onPress={handlePress}`, where `function handlePress(e: React.MouseEvent<HTMLButtonElement>) { e.currentTarget.disabled = true }`, and `tsc --strict` rejects the prop with `Types of parameters 'e' and 'e' are incompatible`. Which account of the error is right, and which edit fixes it?
MCQ-9A chat panel renders `<div ref={lastMessageRef} />` after the last message only while `messages.length > 0`, and a "Jump to latest" button calls `lastMessageRef.current.scrollIntoView({ behavior: 'smooth' })`, where `const lastMessageRef = useRef<HTMLDivElement>(null)`. On React 19 with `strict: true`, `tsc --noEmit` reports `'lastMessageRef.current' is possibly 'null'` on that call. Which change clears the error and still behaves correctly when the list is empty?
MCQ-17A checkout service loads FX rates into `const RATES = new Map<string, number>()` and exposes `rateFor(code: string): number` whose body is `return RATES.get(code);`. Callers compute `subtotal * rateFor(cart.currency)`, and an unconfigured currency must fail with that code in the message: an earlier silent 1:1 fallback mispriced 4,100 orders. Turning on `strictNullChecks` makes that `return` line stop compiling. Which change fixes the line and the missing-currency case it hid?
MCQ-18A `Watchlist` panel renders `const first = items[0]; return <h2>{first.symbol}</h2>` where `items: Ticker[]` is the parsed API payload, and it also reads `const cached = bySymbol[symbol]` from a `Record<string, Ticker>`. `tsc --noEmit` passes today, and the panel throws `Cannot read properties of undefined (reading 'symbol')` whenever the API returns an empty list. The team adds exactly one flag to `tsconfig.json` and re-runs `tsc --noEmit`. Which account of the new output is right?
MCQ-19A design system's `Badge` declares `type BadgeProps = { label?: string; tone?: 'info' | 'warn' }`. A page builds `const overrides: { label: string | undefined } = { label: undefined };` and renders `<Badge {...overrides} />`. On the team's tsconfig (`strict: true` plus one extra flag) `tsc --noEmit` rejects the spread; deleting that one flag makes it compile. Which flag is it, and which change is the right fix?
MCQ-11A design system has `const ThemeContext = createContext({} as Theme)`, where `Theme` is `{ colors: { primary: string } }`. `Button` reads `theme.colors.primary`. A Storybook story renders `<Button />` with no provider and crashes with `Cannot read properties of undefined (reading 'primary')`. The team wants that story to fail with an error naming the missing provider, not render unstyled and not crash inside `theme.colors`. Which change does that?
MCQ-4A cart page runs `const cart = JSON.parse(localStorage.getItem('cart:v1') ?? '{}')` and renders `cart.currency.toUpperCase()`. Last month's release started writing `currency` next to `items`; users who have not reloaded since the older release still have `{ items: [...] }` under that key, and their pages crash in render. Which next step makes the type of `cart` match what the browser actually holds for those users?
MCQ-20A `useUser` hook does `const user = (await res.json()) as User`, where `interface User { id: string; name: string; email: string }`, and `ProfileCard` renders `user.email.toLowerCase()`. A backend release now omits `email` for accounts with `emailVerified: false`, and those pages crash while `tsc --noEmit` stays green. The team wants that payload to fail in CI inside the hook, before any component renders, and will not make `email` optional for every caller. Which change does that?
MCQ-12A profile form edits a `User`, `{ id: string; createdAt: Date; name: string; email: string; role: 'admin' | 'user' }`, and must never submit `id` or `createdAt`. The form builds its field list from `keyof UserFormValues` and posts a `UserFormValues`. Which declaration keeps the form in step with `User` while keeping both server-owned fields out of the form's keys and out of the payload?
MCQ-14`api.ts` exports `async function fetchUsers(opts: { page: number }): Promise<User[]>`, and `cache.ts` needs a `Users` alias for the resolved array so it can annotate its module-level store; importing `User` from `types.ts` would close an import cycle, because `types.ts` imports `cache.ts`. Which declaration gives `cache.ts` that alias with no import and no cast?
MCQ-13A form library derives its change-handler props from a field schema. Given `type Fields = { name: string; age: number }`, `<Form>` must accept exactly `{ onNameChange: (v: string) => void; onAgeChange: (v: number) => void }`, so that `<Form onNameChange={(v) => setName(v)} />` infers `v` as `string` and a typo like `onNameChanged` is a compile error. Which alias produces exactly that type?
MCQ-7`Select.tsx` exports a generic listbox as `const Select = <T>(props: Props<T>) => ...`, where `T` is the option value type, and its call sites include `<Select<string> options={labels} />` and `<Select<number> options={ids} />`; `tsc` stops at the `<T>` with `error TS1005: '>' expected`, and the reviewer asks for the smallest edit that clears the error without changing how `T` is inferred at those call sites. Which edit?
MCQ-6A refactor of the shared `withId` helper broke its call sites: `const u = withId(user); u.email` now fails with `Property 'email' does not exist on type '{ id: string }'`, and `renderUser(withId(user))` fails the same way, even though the helper returns the object it was handed. `User` is `{ id: string; name: string; email: string }` and `Order` is `{ id: string; total: number }`; the team still wants `withId({ name: 'Ada' })` to be a compile error. Which declaration gives them all of that?
MCQ-21A team keeps a compile-time regression test in `src/types.test.ts`: the line `// @ts-expect-error` sits directly above `createUser({ name: 42 })`, and CI runs `tsc --noEmit` over that file with `strict` on. A refactor widens `createUser`'s `name` parameter to `string | number`, so the call now type-checks. Which account of the CI run is right?
MCQ-2`@acme/ui` ships a compiled `ButtonProps` and `Button`. Two apps install it and each needs one extra prop (`analyticsId?` in one, `tone?` in the other), while both still render `<Button>` directly and annotate their own props with the exported type. Neither can fork the package or wait for a release, and the package must not be rebuilt. Which declaration in the package, extended by an app in its own repo, adds the field to both the component's props and the exported type?
MCQ-22A design-system package is transpiled by swc and type-checked by `tsc --noEmit`. `src/types.ts` declares `export interface ButtonProps { size: 'sm' | 'lg' }`, and `src/index.ts` re-exports it with `export { ButtonProps } from './types';`. `tsc --noEmit` is clean, but importing the built ESM bundle throws `SyntaxError: The requested module './types' does not provide an export named 'ButtonProps'`. Which change fixes the built output?
MCQ-23`@acme/ui` is built with Vite (esbuild) and its tsconfig sets `isolatedModules: true`. The `Badge` component's `size` prop must accept exactly `'sm' | 'md' | 'lg'`: app teams write `<Badge size="sm" />` in JSX, `size="xl"` must fail `tsc --noEmit`, and the prop must add no JavaScript to the app bundle. Which declaration of `size` meets all of that?
MCQ-25`@acme/charts` ships only `dist/index.js` (ESM) with no `.d.ts`, and there is no `@types/acme__charts` on the registry; `import { lineChart } from '@acme/charts'` fails `tsc --noEmit` with TS7016, and the team needs checked imports for `lineChart` and `sparkline` today. Which change gives real types for those two exports?