Complete the code to create a TypeScript React component in Next.js.
export default function Home() : [1] { return <h1>Welcome to Next.js with TypeScript!</h1> }
The return type of a React component in TypeScript is usually JSX.Element, which represents the rendered UI.
Complete the code to define a typed prop for a Next.js page component.
type Props = { title: string };
export default function Page({ title }: [1]) {
return <h2>{title}</h2>;
}The component expects props of type Props, so the destructured parameter should be typed as Props.
Fix the error in this Next.js API route handler by typing the request parameter.
import type { NextApiRequest, NextApiResponse } from 'next'; export default function handler(req: [1], res: NextApiResponse) { res.status(200).json({ message: 'Hello from API' }); }
Next.js provides the NextApiRequest type for API route request objects, ensuring proper typing.
Fill both blanks to type a Next.js getStaticProps function with typed context and return value.
import type { GetStaticProps, [1] } from 'next'; export const getStaticProps: GetStaticProps = async (context: [2]) => { return { props: { message: 'Static props with TypeScript' } }; };
The GetStaticPropsContext type is used to type the context parameter in getStaticProps for static generation.
Fill all three blanks to type a Next.js API route with typed request, response, and query parameter.
import type { NextApiRequest, NextApiResponse } from 'next'; export default function handler(req: [1], res: [2]) { const { id }: { id: string } = req.[3]; res.status(200).json({ id }); }
The request is typed as NextApiRequest, the response as NextApiResponse, and the query parameters are accessed via req.query.