Əsas məzmuna keç
FrontDepth, Ana səhifə

Mövzuya qayıt

Məşq: React komponentlərini tipləmək

Əvvəlcə cavab ver, sonra müqayisə et. Özünü dürüst qiymətləndir — zəif yerlərin siyahısı buna əsasən qurulur.

3 nəticə sualı · 4 açıq sual

Sualların səviyyəsi

Seçilmiş səviyyəyə qədər olan suallar göstərilir.

Bu səviyyə üçün hələ məşq sualı yoxdur.

  1. Nəticəni təxmin etJunior

    badgeText props obyektini funksiya komponenti kimi destructuring edir və optional props üçün default dəyərlər verir. Konsola nə çıxacaq?

    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. Açıq sualJunior

    Komponentin props-unu, default dəyərli optional props-u və children-i necə tipləyirsən?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  3. Nəticəni təxmin etMiddle

    Action-ların discriminated union-ı ilə tiplənmiş səbət reducer-i birbaşa çağırılır, useReducer onu necə çağırardısa, elə. Sonuncu action JSON-dan oxunur və CartAction-a cast olunur. Konsola nə çıxacaq?

    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. Nəticəni təxmin etMiddle

    JSX atributları yazıldıqları sıra ilə bir props obyektinə kompilyasiya olunur: <button type="button" {...rest} /> { type: "button", ...rest } olur. Bu iki funksiya iki düymə wrapper-inin props obyektlərini qurur. Konsola nə çıxacaq?

    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. Açıq sualMiddle

    DOM elementi və dəyişkən dəyər üçün useRef-i necə tipləyirsən və React 19-un tiplərində nə dəyişdi?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  6. Açıq sualMiddle

    React 19-da bütün native <button> props-unu qəbul edən və ref-i ötürən Button-u necə tipləyərdin?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.

  7. Açıq sualSenior

    Generic komponenti necə tipləyirsən və props üçün discriminated union-dan nə vaxt istifadə edirsən?

    Əvvəlcə özün düşün

    Müsahibədə olduğun kimi cavabı ucadan və ya içində söylə, sonra aç.