cf.completefrontend
TYPESCRIPT INTERVIEWS

TypeScript interview questions

154 questions about TypeScript, the way interviewers ask them: 64 to explain, 45 where you predict what the compiler reports and infers, and 45 where you write the type or function. 52 easy, 71 medium, and 31 hard. Every answer is checked by the TypeScript compiler.

Every question is free to read. Sign in with Google to see the answers: the short version to say in the interview, why it's true, and what they'll ask next.

154 of 154 questions

Type basics

  1. What is type inference, and when should you still write type annotations?ConceptEasy
  2. What types does TypeScript infer for these variables and this function?Predict the typesEasy
  3. What are TypeScript's primitive types, and why write string rather than String?ConceptEasy
  4. What's the difference between any and unknown?ConceptEasy
  5. What is a union type, and why can't you call toUpperCase() on a string | number?ConceptEasy
  6. Type label(size) so it only accepts "small", "medium", or "large".Write the codeEasy
  7. Why does request(method) compile but request(other) doesn't?ConceptMedium
  8. What does TypeScript infer for these array literals?Predict the typesMedium
  9. What's the difference between {}, object, and unknown?ConceptMedium
  10. What are the top and bottom types in TypeScript, and where does any fit?ConceptMedium
  11. With noUncheckedIndexedAccess on, what are these types, what does TypeScript report, and what does it log?Predict the typesMedium
  12. Write toArray(value): take one value or an array of values, and always return an array.Write the codeMedium
  13. Which of these does TypeScript type as never?Predict the typesHard
  14. Write LiteralUnion<T>: suggest some string literals in the editor, but still accept any string.Write the codeHard
  15. Write compact(values): remove the falsy values, and type the result without the falsy types.Write the codeHard

Objects & interfaces

  1. TypeScript is structurally typed. What does that mean?ConceptEasy
  2. All three objects have an extra admin property. Which lines does TypeScript reject?Predict the typesEasy
  3. What's the difference between age?: number and age: number | undefined?ConceptEasy
  4. What is an index signature, and when would you use Record instead?ConceptEasy
  5. Write a Profile type with a read-only id, an optional bio, and any number of named links.Write the codeEasy
  6. What's the difference between an interface and a type alias, and which should you use?ConceptMedium
  7. What does TypeScript report for these conflicting extends and & declarations?Predict the typesMedium
  8. Does readonly stop these mutations? What does TypeScript report, and what does the code log?Predict the typesMedium
  9. Which of these arguments does TypeScript accept for an object parameter, and which for a Record<string, unknown> parameter?Predict the typesMedium
  10. Which object literals does TypeScript reject when the target is a union?Predict the typesMedium
  11. What is declaration merging, and when is it useful?ConceptMedium
  12. Fix the type error in countWords, and make it count every word correctly.Write the codeMedium
  13. Why does Object.keys(point) return string[] instead of (keyof Point)[]?ConceptHard
  14. In an interface, what's the difference between validate(value: string): boolean and validate: (value: string) => boolean?ConceptHard
  15. Type a style object that allows width, height, color, and any custom property starting with --.Write the codeHard

Functions

  1. How do you type a function's parameters and return value, and when should you write the return type?ConceptEasy
  2. What's the difference between x?: number, x = 10, and x: number | undefined as a parameter?ConceptEasy
  3. How do you write the type of a function, and when do you need a call signature instead of an arrow type?ConceptEasy
  4. Which of these callback parameters does TypeScript infer, and which one is an error?Predict the typesEasy
  5. Type formatPrice(amount, currency, decimals) so only amount is required.Write the codeEasy
  6. What's the difference between void and undefined as a return type? What does TypeScript report here, and what does it log?Predict the typesMedium
  7. Why can you pass a callback with fewer parameters than its type declares, but not one with more?ConceptMedium
  8. Why can you spread point into sum but not into move?Predict the typesMedium
  9. Write createCounter(): a function you call to count, with a count property and a reset() method.Write the codeMedium
  10. How do you type this inside a function, and what does "'this' implicitly has type 'any'" mean?ConceptMedium
  11. What are function overloads, and when should you use a union parameter instead?ConceptMedium
  12. Add overloads to on(event, handler) so each event's handler gets the right parameter type.Write the codeMedium
  13. Which of these calls to an overloaded function compile, and what is ReturnType<typeof parse>?Predict the typesHard
  14. Which detached method does TypeScript catch, and what does this log?Predict the typesHard
  15. Type debounce(fn, wait) so the returned function takes exactly the same arguments as fn.Write the codeHard

Narrowing & assertions

  1. What is narrowing in TypeScript, and which checks narrow a type?ConceptEasy
  2. What does TypeScript narrow these typeof and truthiness checks to, and what does the code log?Predict the typesEasy
  3. When do you narrow with in, and when with instanceof?ConceptEasy
  4. What is a discriminated union, and why is it better than an object with optional fields?ConceptEasy
  5. What does as const do to these types, and what does the code log?Predict the typesEasy
  6. Write isShape(value): a type guard for a discriminated union that arrives as unknown.Write the codeMedium
  7. What's the difference between a type guard (x is T) and an assertion function (asserts x is T)?ConceptMedium
  8. Which of these filter callbacks does TypeScript turn into a type predicate?Predict the typesMedium
  9. What's the difference between as, satisfies, and a type annotation?ConceptMedium
  10. Which of these type assertions does TypeScript accept, and what types do they give?Predict the typesMedium
  11. Make area(shape) exhaustive, so adding a shape without handling it is a compile error.Write the codeMedium
  12. Why doesn't ROLES.includes(value) compile here, and how do you write isRole(value) so it narrows?Write the codeMedium
  13. Why does TypeScript forget a narrowing inside a callback?ConceptHard
  14. Which of these indirect checks does TypeScript still narrow on?Predict the typesHard
  15. Can a type predicate lie? What's wrong with this isNonEmptyString guard?ConceptHard

Generics

  1. Why use generics instead of any?ConceptEasy
  2. What does TypeScript infer for T in each of these calls?Predict the typesEasy
  3. Make createState(initial) generic, so get and set use the type of the initial value.Write the codeEasy
  4. What does T extends … mean on a type parameter, and why do you need it?ConceptEasy
  5. How do you write a generic interface or type alias, and when do you have to pass its type argument?ConceptEasy
  6. Which of these getProperty calls compile, and what does each return?Predict the typesMedium
  7. What are default type parameters, and when does TypeScript use the default?ConceptMedium
  8. How do you write a generic class, and why can't its static members use the type parameter?ConceptMedium
  9. Why is a type parameter that appears only once in a signature a code smell?ConceptMedium
  10. Write pick(obj, keys): return a new object with only the listed keys, typed to match.Write the codeMedium
  11. Write a typed groupBy(items, getKey) that groups items into an object by the key the callback returns.Write the codeMedium
  12. What does the const modifier on a type parameter change in these calls?Predict the typesMedium
  13. What does NoInfer<T> do, and when do you need it?ConceptMedium
  14. What happens when you pass only some of a generic function's type arguments?Predict the typesHard
  15. Write a typed event emitter: on and emit should only accept the events, and the arguments, of an event map.Write the codeHard
  16. fetchJson<T>(url) compiles, but it lies about T. Rewrite it so the type is backed by a runtime check.Write the codeHard

Type manipulation

  1. What do keyof and typeof do in a type, and what does keyof typeof config give you?ConceptEasy
  2. What do these indexed access types resolve to, and which lines does TypeScript reject?Predict the typesEasy
  3. Write TupleToUnion<T>, and use it to derive a Role union from a roles array.Write the codeEasy
  4. What is a conditional type, and how does T extends U ? X : Y decide?ConceptEasy
  5. What are template literal types, and what do Uppercase and Capitalize do?ConceptEasy
  6. What do these conditional types produce when you pass them a union?Predict the typesMedium
  7. What does infer do in a conditional type?ConceptMedium
  8. Write UnwrapPromise<T>: the value a promise resolves to, however deeply it's nested.Write the codeMedium
  9. What is a mapped type, and what do -readonly, -?, and as do in one?ConceptMedium
  10. Write Getters<T>: turn { name: string } into { getName: () => string }.Write the codeMedium
  11. Write KeysOfType<T, V>: the keys of T whose values are assignable to V.Write the codeMedium
  12. Write EventHandlers<T>: from an event map like { click: Point }, build optional onClick-style handler props.Write the codeMedium
  13. What do these template literal types infer?Predict the typesHard
  14. How do you write a recursive type in TypeScript, and what are its limits?ConceptHard
  15. Write PathKeys<T>: every dot path into a nested object, like "account.address.city".Write the codeHard

Utility types

  1. What are utility types, and what do Partial, Required, and Readonly do?ConceptEasy
  2. When would you use Pick, and when Omit?ConceptEasy
  3. What does TypeScript report and infer for these Record types?Predict the typesEasy
  4. What do Exclude, Extract, and NonNullable produce for these types?Predict the typesEasy
  5. Write MyPartial<T> without using the built-in Partial.Write the codeEasy
  6. Write MyPick<T, K>: keep only the keys K of T, and reject keys T doesn't have.Write the codeMedium
  7. Write MyReadonly<T>, and its opposite, Mutable<T>, which removes readonly.Write the codeMedium
  8. What do ReturnType, Parameters, Awaited, and InstanceType give for this code?Predict the typesMedium
  9. Type updateUser(user, changes) so changes can hold any editable field, but never id or createdAt.Write the codeMedium
  10. Is Partial<User> a good type for a form's state? What can go wrong?ConceptMedium
  11. Write MyOmit<T, K> without the built-in Omit, and make it reject keys that T doesn't have.Write the codeMedium
  12. Write MyReturnType<T> without the built-in ReturnType.Write the codeMedium
  13. Write DeepPartial<T> for nested settings, keeping functions callable and arrays as arrays.Write the codeHard
  14. Write DeepReadonly<T>: make every nested property and array readonly, but keep functions callable.Write the codeHard
  15. Write MyAwaited<T> so it unwraps any thenable the way await does, not only a Promise.Write the codeHard
  16. What happens when you Omit a key from a discriminated union?Predict the typesHard

Classes

  1. What do public, private, and protected mean on a class member?ConceptEasy
  2. What's the difference between implements and extends on a class?ConceptEasy
  3. Which writes to these readonly properties does TypeScript reject, and what does the code log?Predict the typesEasy
  4. What are parameter properties, and why does erasableSyntaxOnly reject them?ConceptEasy
  5. Make balance readable but not writable from outside the class.Write the codeEasy
  6. Which of these properties does TypeScript say are never initialized?Predict the typesMedium
  7. A class has a private property and a #private field. What does TypeScript report, and what does the code log?Predict the typesMedium
  8. When would you use an abstract class instead of an interface?ConceptMedium
  9. With noImplicitOverride on, which lines in this subclass does TypeScript report?Predict the typesMedium
  10. Which of these values does TypeScript accept as class instances, and what does the code log?Predict the typesMedium
  11. What's the difference between the types User and typeof User?ConceptMedium
  12. Fix the builder so chained calls keep the subclass type.Write the codeMedium
  13. Type instantiate(Ctor, ...args) so it accepts any class with matching arguments and returns its instance.Write the codeMedium
  14. How are TypeScript 5's standard decorators different from experimentalDecorators?ConceptHard
  15. Why does user.name log undefined here, when the base constructor set it?Predict the typesHard
  16. Type the Tagged mixin so the class it returns keeps the base class's members and constructor.Write the codeHard

Modules & configuration

  1. What does import type do, and how is it different from a regular import?ConceptEasy
  2. What is a .d.ts file, and where do the types for an npm package come from?ConceptEasy
  3. What does strict turn on, and which flags would you add on top of it?ConceptEasy
  4. What's the difference between target, lib, and module in a tsconfig?ConceptEasy
  5. How can TypeScript check a JavaScript file without converting it to TypeScript?ConceptEasy
  6. Enums, union types, or as const objects: which would you use, and why?ConceptMedium
  7. Replace this Size enum with an as const object, and add an isSize guard for strings that come from outside.Write the codeMedium
  8. With verbatimModuleSyntax on, which of these imports and exports does TypeScript reject?Predict the typesMedium
  9. tiny-slug ships no types. Write a declaration so import { slugify } from "tiny-slug" is fully typed.Write the codeMedium
  10. This file should add analytics to window, but TypeScript rejects it. Fix it, and declare a global __APP_VERSION__ too.Write the codeMedium
  11. Vite, esbuild, and Node.js run TypeScript without checking it. So what catches the type errors?ConceptMedium
  12. With erasableSyntaxOnly on, which of these lines does TypeScript reject?Predict the typesMedium
  13. This .d.ts file was meant to add brandColor to shop-ui's Theme. What does TypeScript report in app.ts?Predict the typesHard
  14. Under module: nodenext, which of these imports does TypeScript reject, and why write .js for a .ts file?Predict the typesHard
  15. Type checking a large repo has become slow. What do skipLibCheck, incremental, and project references do about it?ConceptHard

TypeScript in practice

  1. What types do these DOM queries return, and what does TypeScript report?Predict the typesEasy
  2. In these event listeners, why is event.target.value an error, and what type is event?Predict the typesEasy
  3. Why is await response.json() as User unsafe, and what should you do instead?ConceptEasy
  4. Write isUser(value): a type guard that checks parsed JSON really is a User.Write the codeEasy
  5. Why is the catch variable unknown in TypeScript, and how do you handle errors safely?ConceptEasy
  6. How do you type a React component's props, children, and event handlers?ConceptEasy
  7. What types does TypeScript give this async code, and which line is an error?Predict the typesMedium
  8. Write a Result<T, E> type and attempt(fn), which returns errors as values instead of throwing them.Write the codeMedium
  9. How do you type useState, useReducer, and useRef in React?ConceptMedium
  10. Write branded UserId and ProductId types, so one can't be passed where the other is expected.Write the codeMedium
  11. How would you migrate a large JavaScript codebase to TypeScript without stopping feature work?ConceptMedium
  12. How do you read a long TypeScript error message, like the one this call gives?ConceptMedium
  13. What TypeScript habits do you follow on a real project?ConceptMedium
  14. Which of these event handler assignments does TypeScript reject, and why does the method version compile?Predict the typesHard
  15. This compiles with no errors. What does it log, and where does TypeScript's type safety break down?Predict the typesHard
  16. Write a type-safe API client from a route map: each route takes the right body and resolves to the right response type.Write the codeHard