import React from 'react'; interface GreetingProps { name: string; age?: number; } const Greeting: React.FC<GreetingProps> = ({ name, age }) => { return ( <div> <p>Hello, {name}!</p> {age !== undefined && <p>You are {age} years old.</p>} </div> ); }; export default function Page() { return <Greeting name="Alice" />; }
The age prop is optional. Since it is not passed, the condition {age && ...} evaluates to false, so the age paragraph is not rendered. The component renders only the greeting with the name.
Option D correctly imports the types and uses res.status(200).json() to send JSON. Option D lacks types. Option D uses res.send which is not recommended. Option D incorrectly returns a string instead of sending a response.
import { GetStaticProps } from 'next'; interface Props { count: number; } export default function Counter({ count }: Props) { return <p>Count: {count}</p>; } export const getStaticProps: GetStaticProps<Props> = async () => { return { props: { count: 10 } }; };
The count prop is typed as number, and getStaticProps returns it as a number 10. This is correct and causes no TypeScript error.
import React, { useState } from 'react'; export default function Counter() { const [count, setCount] = useState<number>(0); return ( <div> <p>Count: {count}</p> <button onClick={() => setCount(count + 1)}>Increment</button> </div> ); }
The initial count is 0. Clicking the button calls setCount(count + 1), updating count to 1, which is displayed.
Next.js automatically generates a default tsconfig.json file when it detects a TypeScript file for the first time, simplifying setup.