Skip to content
FrontDepth, Home

Back to the topic

Practice: Typing React components

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 outputJunior

    badgeText destructures its props object the way a function component does, with defaults for the optional props. What does it log?

    TypeScript
    type BadgeProps = {
      label: string;
      tone?: "neutral" | "success";
      max?: number | null;
    };
    
    function badgeText({ label, tone = "neutral", max = 99 }: BadgeProps): string {
      return `${label} ${tone} ${max}`;
    }
    
    console.log(badgeText({ label: "a" }));
    console.log(badgeText({ label: "b", tone: undefined }));
    console.log(badgeText({ label: "c", max: null }));
    console.log(badgeText({ label: "d", tone: "success", max: 0 }));
  2. Open questionJunior

    How do you type a component's props, optional props with defaults, and children?

    Think it through first

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

  3. Predict the outputMiddle

    A cart reducer typed with a discriminated union of actions, called directly, as useReducer would call it. The last action is read from JSON and cast to CartAction. What does it log?

    TypeScript
    type Line = { id: string; qty: number };
    type CartAction =
      | { type: "added"; id: string }
      | { type: "removed"; id: string }
      | { type: "cleared" };
    
    function cartReducer(lines: Line[], action: CartAction): Line[] {
      switch (action.type) {
        case "added":
          return lines.some((l) => l.id === action.id)
            ? lines.map((l) => (l.id === action.id ? { ...l, qty: l.qty + 1 } : l))
            : [...lines, { id: action.id, qty: 1 }];
        case "removed":
          return lines.filter((l) => l.id !== action.id);
        case "cleared":
          return [];
        default: {
          const unhandled: never = action;
          throw new Error("Unknown action " + JSON.stringify(unhandled));
        }
      }
    }
    
    const show = (lines: Line[]) => lines.map((l) => l.id + "x" + l.qty).join(",") || "empty";
    
    let cart: Line[] = [];
    cart = cartReducer(cart, { type: "added", id: "tea" });
    cart = cartReducer(cart, { type: "added", id: "cup" });
    cart = cartReducer(cart, { type: "added", id: "tea" });
    console.log(show(cart));
    cart = cartReducer(cart, { type: "removed", id: "cup" });
    console.log(show(cart));
    
    const fromStorage = JSON.parse('{"type":"emptied"}') as CartAction;
    try {
      cart = cartReducer(cart, fromStorage);
    } catch (error) {
      console.log((error as Error).message);
    }
    console.log(show(cart));
  4. Predict the outputMiddle

    JSX attributes compile to one props object, in the order they are written: <button type="button" {...rest} /> becomes { type: "button", ...rest }. These two functions build the props objects of two button wrappers. What does it log?

    TypeScript
    type ButtonProps = {
      type?: "button" | "submit" | "reset";
      className?: string;
      disabled?: boolean;
    };
    
    // The props object that <button type="button" {...rest} className={...} /> creates
    function buttonProps({ className, ...rest }: ButtonProps) {
      return { type: "button", ...rest, className: ["btn", className].filter(Boolean).join(" ") };
    }
    
    // The props object that <button {...props} type="button" /> creates
    function lockedProps(props: ButtonProps) {
      return { ...props, type: "button" };
    }
    
    console.log(buttonProps({}).type);
    console.log(buttonProps({ type: "submit" }).type);
    console.log(buttonProps({ type: undefined }).type);
    console.log(lockedProps({ type: "submit" }).type);
    console.log(buttonProps({ className: "wide" }).className);
  5. Open questionMiddle

    How do you type useRef for a DOM element and for a mutable value, and what changed in React 19's types?

    Think it through first

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

  6. Open questionMiddle

    How would you type a Button that accepts every native <button> prop and passes a ref through in React 19?

    Think it through first

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

  7. Open questionSenior

    How do you type a generic component, and when do you use a discriminated union for props?

    Think it through first

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