TypeScript – Front End interview prep
261 interview-style multiple-choice questions on TypeScript, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 51 key terms with the flashcards first, then test yourself.
Take the practice test · Flashcards · All study tools
What it covers
- 5.1 Basic types and inference
- 5.2 Objects, interfaces and type aliases
- 5.3 Unions, narrowing and control flow
- 5.4 Functions, overloads and this
- 5.5 Generics
- 5.6 keyof, conditional, mapped and template literal types
- 5.7 Built-in utility types
- 5.8 Classes, enums, modules and declarations
- 5.9 tsconfig, tooling and TypeScript in practice
Key terms
5.1 Basic types and inference
- Type erasure
- TypeScript types are removed when compiling, so they don't exist at runtime
- unknown
- The type-safe top type: anything can be assigned, but it must be narrowed before use
- never
- The type with no values, used for impossible cases and exhaustiveness checks
- Structural typing
- Compatibility decided by shape, not by declared name
- Literal type
- Type that's exactly one value, like "GET"
- Widening
- Generalizing a literal type to its base type
- Contextual typing
- Inferring a type from where a value is used
- Top type
- unknown, which every type is assignable to
5.2 Objects, interfaces and type aliases
- Index signature
- Type for objects with arbitrary keys
- Declaration merging
- Same-named interfaces combining members
- Excess property check
- Error for extra keys on fresh object literals
- Branded type
- Primitive tagged to stop mixing similar values
5.3 Unions, narrowing and control flow
- Narrowing
- Refining a union to a more specific type with checks like typeof, instanceof or in
- Discriminated union
- Union of objects sharing a literal tag property used to narrow
- Type predicate
- A return type like x is User that narrows when the function returns true
- Non-null assertion (!)
- Tells the compiler a value isn't null or undefined, with no runtime check
- Exhaustiveness check
- Assigning leftovers to never to catch unhandled cases
- Assertion function
- Function declared asserts x is T that throws if false
- Control flow analysis
- Tracking types through branches and assignments
5.4 Functions, overloads and this
- Overload
- One of several call signatures for a function
- this parameter
- Fake first parameter declaring this's type
- Type guard
- Function returning x is T
- NoInfer
- Utility that blocks inference from a position
5.5 Generics
- Generic
- A type parameter that lets code work over many types while keeping them linked
- keyof
- Produces a union of an object type's property names
- Conditional type
- A type-level if/else: T extends U ? X : Y
- Mapped type
- Creates an object type by iterating over keys, like Partial<T>
- Indexed access type
- T[K], the type of property K in T
- Default type parameter
- Type used when none is given
- Variance
- How subtyping of T affects Box<T>
- Generic constraint
- extends clause limiting a type parameter
5.6 keyof, conditional, mapped and template literal types
- Template literal type
- String type built from other types
- Distributive conditional
- Conditional applied to each union member
- Key remapping
- as clause in a mapped type to rename or drop keys
- infer
- Captures part of a matched type in a conditional
5.7 Built-in utility types
- Partial<T>
- Makes every property of T optional
- Pick / Omit
- Keep or remove the listed keys of a type
- Record<K, V>
- Object type with keys K and values V
- Declaration file (.d.ts)
- Types for JavaScript code, with no implementation
- Omit
- Utility removing keys from a type
- Awaited
- Utility unwrapping promise types recursively
- NonNullable
- Utility removing null and undefined
- Intrinsic type
- Utility implemented by the compiler itself
5.8 Classes, enums, modules and declarations
- Parameter property
- Constructor parameter that declares a field
- const enum
- Enum inlined at use sites
- Type stripping
- Removing type annotations without type checking
- using declaration
- Variable disposed via Symbol.dispose at scope end
5.9 tsconfig, tooling and TypeScript in practice
- tsconfig.json
- File that configures a TypeScript project
- isolatedModules
- Option ensuring each file can be transpiled alone
- Project references
- Splitting a codebase into incrementally built projects
- Runtime validation
- Checking external data at runtime
Sample practice questions
What happens to TypeScript types when the code runs?
- They're compiled into JavaScript classes with validators
- They're kept as comments that the browser validates
- The runtime checks every assignment against its type
- They're erased, so there are no runtime type checks
Answer: D. They're erased, so there are no runtime type checks TypeScript only checks at build time; validate untrusted data (like API responses) at runtime with code or a schema library.
When should you prefer `interface` over `type`?
- Only when the object has methods, not plain data
- For object shapes you'll extend or merge, like public contracts
- Never; `interface` is deprecated in favor of `type`
- Only for describing classes, never plain objects
Answer: B. For object shapes you'll extend or merge, like public contracts Both describe object shapes; only interfaces merge across declarations and only type aliases can express unions.
How do you narrow a `string | number` parameter to call `.toUpperCase()` safely?
- Nothing; TypeScript narrows it automatically
- Cast it with `value as string` every time
- Check `value instanceof String` first
- Check `typeof value === "string"` first
Answer: D. Check `typeof value === "string"` first `typeof`, `instanceof`, `in` and equality checks all narrow unions.
How do you type an optional parameter?
- `function f(?x: number)`
- `function f(x: optional number)`
- `function f(x?: number)`
- `function f(x: number?)`
Answer: C. `function f(x?: number)` Optional parameters must come after required ones.
Why write `function first<T>(items: T[]): T | undefined` instead of using `any[]`?
- Generics add runtime checks on the array
- `any[]` isn't allowed as a parameter type
- Generics make the function run faster at runtime
- The return type follows the input, keeping type safety
Answer: D. The return type follows the input, keeping type safety Generics let one function work for many types without losing information.
What does `keyof` give you?
- A union of an object type's property names
- The number of keys in an object type
- A union of an object type's property values
- A runtime array of an object's keys
Answer: A. A union of an object type's property names Combined with indexed access, `T[K]` gives the type of a property.
What does `Partial<User>` produce?
- The `User` type with every property optional
- `User` with every property required
- A readonly copy of `User`
- Only the first half of `User`'s properties
Answer: A. The `User` type with every property optional Common for update functions that accept a subset of fields.
What is a `.d.ts` file?
- A compiled bundle of TypeScript output
- A declaration file: types only, no implementation
- A test file that checks type definitions
- A database schema file used by the compiler
Answer: B. A declaration file: types only, no implementation Library types come from `.d.ts` files, often published under `@types/...`.
What does `strictNullChecks` change?
- It forbids writing `null` anywhere in the code
- It turns off type inference for variables
- It makes every object property optional
- `null` and `undefined` must be handled explicitly
Answer: D. `null` and `undefined` must be handled explicitly Part of `strict`; it catches a huge class of "cannot read property of undefined" bugs.
What is the difference between `any` and `unknown`?
- They're the same type, kept under two names for compatibility
- `unknown` must be narrowed before use; `any` turns checking off
- `unknown` turns checking off; `any` must be narrowed before use
- `unknown` can only hold `null` or `undefined` values
Answer: B. `unknown` must be narrowed before use; `any` turns checking off Prefer `unknown` for values of unknown shape, such as `JSON.parse` results or caught errors.
More Front End
- HTML semantics & accessibility
- CSS: box model, Flexbox, Grid
- JavaScript fundamentals
- Async JavaScript & the event loop
- React: components, hooks & rendering
- State management: Redux, Zustand, context
- Angular and Vue basics
- Browser internals & rendering
- Web performance
- Build tools & bundlers
- Front-end testing
- Front-end security
- Mobile & cross-platform