Skip to content
FrontDepth, Home

Back to the topic

Practice: Utility, mapped and conditional types

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

3 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputMiddle

    settings is typed Readonly<Settings>, and the code type-checks under strict. What does it log?

    TypeScript
    type Settings = { theme: string; tags: string[] };
    
    const settings: Readonly<Settings> = { theme: "dark", tags: ["news"] };
    settings.tags.push("sport");
    
    const copy: Settings = { ...settings, theme: "light" };
    copy.tags.push("music");
    
    console.log(settings.theme);
    console.log(settings.tags.length);
    console.log(copy.tags === settings.tags);
    console.log(Object.isFrozen(settings));
  2. Predict the outputMiddle

    applyPatch merges a Partial<User> into a User. The caller passes { email: undefined }, and the code type-checks under strict. What does it log?

    TypeScript
    type User = { name: string; email: string };
    
    function applyPatch(user: User, patch: Partial<User>): User {
      return { ...user, ...patch };
    }
    
    const user: User = { name: "Aysel", email: "aysel@example.com" };
    const next = applyPatch(user, { email: undefined });
    
    console.log(Object.keys(next).join(","));
    console.log(typeof next.email);
    console.log(JSON.stringify(next));
  3. Open questionMiddle

    How are Partial, Pick, Record and Omit defined, and what do the definitions tell you about how they behave?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  4. Open questionMiddle

    How do typeof, keyof and indexed access types help you keep one source of truth? Give an example.

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  5. Predict the outputSenior

    Getters<T> is a mapped type with key remapping: for { id, name } it describes { getId, getName }. toGetters builds the object at run time and returns it with a cast. The code type-checks under strict. What does it log?

    TypeScript
    type Getters<T> = {
      [K in keyof T & string as `get${Capitalize<K>}`]: () => T[K];
    };
    
    function toGetters<T extends Record<string, unknown>>(obj: T): Getters<T> {
      const out: Record<string, () => unknown> = {};
      for (const key of Object.keys(obj)) {
        out["get" + key.toUpperCase()] = () => obj[key];
      }
      return out as Getters<T>;
    }
    
    const user = toGetters({ id: 7, name: "Aysel" });
    
    console.log(Object.keys(user).join(","));
    console.log(typeof user.getName);
    console.log("getNAME" in user);
  6. Open questionSenior

    What is a distributive conditional type, and when do you turn distribution off?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  7. Open questionSenior

    When is type-level code too clever for a codebase, and what do you do about it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.