0
0
NextJSframework~20 mins

TypeScript support in Next.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js TypeScript Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Next.js TypeScript component?
Consider this Next.js functional component written in TypeScript. What will it render on the page?
NextJS
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" />;
}
AComponent throws a TypeScript error because age is missing
B<div><p>Hello, Alice!</p><p>You are undefined years old.</p></div>
C<div><p>Hello, Alice!</p></div>
D<div><p>Hello, Alice!</p><p>You are 0 years old.</p></div>
Attempts:
2 left
💡 Hint
Check how optional props work in TypeScript and React.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a Next.js API route handler in TypeScript?
You want to create a Next.js API route with TypeScript that returns JSON { message: 'Hello' }. Which code snippet is correct?
A
import { NextApiRequest, NextApiResponse } from 'next';

export default (req: NextApiRequest, res: NextApiResponse) =&gt; {
  res.send({ message: 'Hello' });
}
B
export default function handler(req, res) {
  res.json({ message: 'Hello' });
}
C
import type { NextApiRequest, NextApiResponse } from 'next';

export default function handler(req: NextApiRequest, res: NextApiResponse): string {
  return JSON.stringify({ message: 'Hello' });
}
D
import type { NextApiRequest, NextApiResponse } from 'next';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  res.status(200).json({ message: 'Hello' });
}
Attempts:
2 left
💡 Hint
Check the correct import and response method for Next.js API routes in TypeScript.
🔧 Debug
advanced
2:00remaining
Why does this Next.js TypeScript page cause a type error?
Examine this Next.js page component. Why does it cause a TypeScript error?
NextJS
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
    }
  };
};
AType error because Counter component is missing React.FC type annotation
BNo error, the code is valid and runs correctly
CType error because getStaticProps must return a Promise, but it returns an object
DType error because count is declared as number but getStaticProps returns it as string
Attempts:
2 left
💡 Hint
Check the type of count in props vs the value returned from getStaticProps.
state_output
advanced
2:00remaining
What is the output of this Next.js TypeScript component with useState?
Look at this Next.js component using TypeScript and React hooks. What will it display after clicking the button once?
NextJS
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>
  );
}
ACount: 1
BCount: 0
CCount: NaN
DType error because useState is missing generic type
Attempts:
2 left
💡 Hint
Think about how useState updates state and the initial value type.
🧠 Conceptual
expert
2:00remaining
Which statement about TypeScript support in Next.js is TRUE?
Select the correct statement about how Next.js supports TypeScript out of the options below.
ANext.js automatically creates a default tsconfig.json on first TypeScript file detection
BNext.js requires manual configuration of tsconfig.json before using TypeScript
CNext.js does not support incremental static regeneration with TypeScript
DNext.js only supports TypeScript in API routes, not in pages or components
Attempts:
2 left
💡 Hint
Think about how Next.js handles TypeScript setup for beginners.