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

  1. 5.1 Basic types and inference
  2. 5.2 Objects, interfaces and type aliases
  3. 5.3 Unions, narrowing and control flow
  4. 5.4 Functions, overloads and this
  5. 5.5 Generics
  6. 5.6 keyof, conditional, mapped and template literal types
  7. 5.7 Built-in utility types
  8. 5.8 Classes, enums, modules and declarations
  9. 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

  1. What happens to TypeScript types when the code runs?

    1. They're compiled into JavaScript classes with validators
    2. They're kept as comments that the browser validates
    3. The runtime checks every assignment against its type
    4. 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.

  2. When should you prefer `interface` over `type`?

    1. Only when the object has methods, not plain data
    2. For object shapes you'll extend or merge, like public contracts
    3. Never; `interface` is deprecated in favor of `type`
    4. 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.

  3. How do you narrow a `string | number` parameter to call `.toUpperCase()` safely?

    1. Nothing; TypeScript narrows it automatically
    2. Cast it with `value as string` every time
    3. Check `value instanceof String` first
    4. Check `typeof value === "string"` first

    Answer: D. Check `typeof value === "string"` first `typeof`, `instanceof`, `in` and equality checks all narrow unions.

  4. How do you type an optional parameter?

    1. `function f(?x: number)`
    2. `function f(x: optional number)`
    3. `function f(x?: number)`
    4. `function f(x: number?)`

    Answer: C. `function f(x?: number)` Optional parameters must come after required ones.

  5. Why write `function first<T>(items: T[]): T | undefined` instead of using `any[]`?

    1. Generics add runtime checks on the array
    2. `any[]` isn't allowed as a parameter type
    3. Generics make the function run faster at runtime
    4. 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.

  6. What does `keyof` give you?

    1. A union of an object type's property names
    2. The number of keys in an object type
    3. A union of an object type's property values
    4. 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.

  7. What does `Partial<User>` produce?

    1. The `User` type with every property optional
    2. `User` with every property required
    3. A readonly copy of `User`
    4. 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.

  8. What is a `.d.ts` file?

    1. A compiled bundle of TypeScript output
    2. A declaration file: types only, no implementation
    3. A test file that checks type definitions
    4. 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/...`.

  9. What does `strictNullChecks` change?

    1. It forbids writing `null` anywhere in the code
    2. It turns off type inference for variables
    3. It makes every object property optional
    4. `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.

  10. What is the difference between `any` and `unknown`?

    1. They're the same type, kept under two names for compatibility
    2. `unknown` must be narrowed before use; `any` turns checking off
    3. `unknown` turns checking off; `any` must be narrowed before use
    4. `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.

Practice all 261 questions

More Front End