QuizzesTypeScript with React — Concept Check
intermediate
javascript

TypeScript with React — Concept Check

Questions on TypeScript with React. Typing props, state, events, refs and children. This is where most TypeScript in a frontend job actually lives.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on TypeScript with React — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a React component that uses a state initialized as null. Which of the following is the correct way to type this state in TypeScript?

    • Aconst [user, setUser] = useState<User | null>(null)
    • Bconst [user, setUser] = useState<null>(null)
    • Cconst [user, setUser] = useState<User | undefined>(null)
    • Dconst [user, setUser] = useState<User>(null)
  2. 2.A developer is using React with TypeScript and wants to extend native button props in a custom Button component. Which pattern should they use?

    • Ainterface ButtonProps extends React.HTMLAttributes<'button'>
    • Binterface ButtonProps extends React.ComponentProps<'button'>
    • Cinterface ButtonProps extends React.ComponentPropsWithoutRef<'button'>
    • Dinterface ButtonProps extends React.HTMLProps<'button'>
  3. 3.In a React component, you have an input field with an onChange handler. How should you type the event parameter when the handler is extracted?

    • Aconst onChange = (e: React.FormEvent<HTMLInputElement>) => { ... }
    • Bconst onChange = (e: React.ChangeEvent<HTMLInputElement>) => { ... }
    • Cconst onChange = (e: Event) => { ... }
    • Dconst onChange = (e: React.MouseEvent<HTMLInputElement>) => { ... }
  4. 4.A developer is using useRef to manage a mutable timer variable in a React component. Which is the correct way to initialize this ref?

    • Aconst timer = useRef<number | null>(null)
    • Bconst timer = useRef<number | undefined>(undefined)
    • Cconst timer = useRef<number>(0)
    • Dconst timer = useRef<number>(null)
  5. 5.You want to create a generic List component in React that renders a list of items. How should you type the render prop?

    • Arender: (item: T) => React.ReactNode
    • Brender: (item: T) => JSX.Element
    • Crender: (item: T) => React.ReactElement
    • Drender: (item: T) => void
  6. 6.What is the advantage of using as const with a tuple returned from a custom hook in TypeScript?

    • AIt makes the tuple readonly, preventing unintended mutations.
    • BIt converts the tuple to an array, allowing more flexibility.
    • CIt automatically infers types for the tuple elements.
    • DIt allows the tuple to be modified without errors.
  7. 7.A developer is using a context pattern in React with TypeScript. Why should they throw an error if the context is undefined?

    • ATo catch runtime errors in the component tree.
    • BTo optimize performance by reducing checks.
    • CTo prevent TypeScript from inferring incorrect types.
    • DTo ensure the hook is used within the correct provider.
  8. 8.In a React component, why should you avoid using React.FC for typing props?

    • AIt requires manual prop type definitions.
    • BIt doesn't provide type safety for event handlers.
    • CIt implicitly adds a children prop, complicating generics.
    • DIt doesn't support default props.