interface BaseProps {
id: string;
}
interface AdvancedProps extends BaseProps {
date: Date;
}
const Component: React.FC<AdvancedProps> = ({ id, date }) => (
<div>{id}: {date.toISOString()}</div>
);
React with TypeScript Quiz
Want to learn more than this quiz offers you? Have a look at my Frontend web
development courses.
Create an account and save your quiz results
Login and save your results
OR
Question 1/15
How can you extend interfaces in TypeScript when creating a React component?
interface BaseProps {
id: string;
}
interface AdvancedProps extends BaseProps {
date: Date;
}
const Component: React.FC<AdvancedProps> = ({ id, date }) => (
<div>{id}: {date.toISOString()}</div>
);
Select your answer
Question 2/15
In TypeScript, how do you prevent assigning `undefined` to a prop?
interface MyComponentProps {
name: string;
}
const MyComponent: React.FC<MyComponentProps> = ({ name }) => {
return <div>Hello, {name}</div>;
};
Select your answer
Question 3/15
How do you enforce that a certain number of times a component rerenders when a particular prop changes in TypeScript?
class MyComponent extends React.Component<{ inputValue: number }> {
componentDidUpdate(prevProps: { inputValue: number }) {
if (prevProps.inputValue !== this.props.inputValue) {
console.log('Rerendered!');
}
}
}
Select your answer
Question 4/15
Which hook is used to set up initial state in a TypeScript functional component?
const [count, setCount] = useState<number>(0);
Select your answer
Question 5/15
In TypeScript, what is a Discriminated Union and how is it useful in React?
type LoadingState = {
state: 'loading';
};
type SuccessState = {
state: 'success';
response: string;
};
type ErrorState = {
state: 'error';
error: Error;
};
interface StateContainer {
stateObject: LoadingState | SuccessState | ErrorState;
}
const Component: React.FC<StateContainer> = ({ stateObject }) => {
switch (stateObject.state) {
case 'loading':
return <div>Loading...</div>;
case 'success':
return <div>{stateObject.response}</div>;
case 'error':
return <div>Error: {stateObject.error.message}</div>;
default:
return null;
}
};
Select your answer
Question 6/15
In a TypeScript React component, how can you specify that a prop is optional?
interface MyComponentProps {
name?: string;
}
const MyComponent: React.FC<MyComponentProps> = ({ name }) => {
return <div>{name}</div>;
};
Select your answer
Question 7/15
How do you define default props in a React component using TypeScript?
interface MyComponentProps {
name?: string;
}
const MyComponent = ({ name = "Guest" }: MyComponentProps) => {
return <div>Hello, {name}</div>;
};
Select your answer
Question 8/15
Which TypeScript utility can you use to define only some of the properties of an interface as required?
interface Person {
name: string;
age: number;
address?: string;
}
const person: Required<Pick<Person, 'name' | 'age'>> = { name: 'Alice', age: 30 };
Select your answer
Question 9/15
What is the `key` prop used for in React lists?
const items = ['a', 'b', 'c'];
const listItems = items.map((item, index) => (
<li key={index}>{item}</li>
));
Select your answer
Question 10/15
Given the following TypeScript code, what type does the `setColor` function accept?
const [color, setColor] = useState<string | null>(null);
Select your answer
Question 11/15
How do you type a component that takes a prop function with a return type of `Promise` using TypeScript with React?
interface AsyncComponentProps {
fetchData: () => Promise<void>;
}
const AsyncComponent: React.FC<AsyncComponentProps> = ({ fetchData }) => {
useEffect(() => {
fetchData();
}, [fetchData]);
return <div>Data has been fetched.</div>;
};
Select your answer
Question 12/15
How can you ensure a component in TypeScript only accepts props of a specific shape using TypeScript?
interface ButtonProps {
label: string;
onClick: () => void;
}
const Button: React.FC<ButtonProps> = ({ label, onClick }) => (
<button onClick={onClick}>{label}</button>
);
Select your answer
Question 13/15
What is the purpose of the `React.FC` type in a TypeScript React component?
const MyComponent: React.FC = () => {
return <div>Hello, World!</div>;
};
Select your answer
Question 14/15
How can you define and use a custom hook with TypeScript in a React component?
function useCustomHook() {
const [value, setValue] = useState<string>("");
return { value, setValue };
}
const MyComponent: React.FC = () => {
const { value, setValue } = useCustomHook();
return <input value={value} onChange={e => setValue(e.target.value)} />;
};
Select your answer
Question 15/15
How do you define a prop that accepts children in TypeScript with React?
interface MyComponentProps {
children?: React.ReactNode;
}
const MyComponent: React.FC<MyComponentProps> = ({ children }) => {
return <div>{children}</div>;
};
Select your answer
Your Results
You did not answer any questions correctly.
Your Answers
Question 1/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
How can you extend interfaces in TypeScript when creating a React component?
Available answers
In TypeScript, interfaces can be extended using the
extends
keyword, which allows one interface to inherit and augment another.
Question 2/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
In TypeScript, how do you prevent assigning `undefined` to a prop?
interface MyComponentProps {
name: string;
}
const MyComponent: React.FC<MyComponentProps> = ({ name }) => {
return <div>Hello, {name}</div>;
};
Available answers
In TypeScript, to make a parameter non-optional (i.e., prevent it from being
undefined
), you simply omit the ?
modifier in the type interface definition.
Question 3/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
How do you enforce that a certain number of times a component rerenders when a particular prop changes in TypeScript?
class MyComponent extends React.Component<{ inputValue: number }> {
componentDidUpdate(prevProps: { inputValue: number }) {
if (prevProps.inputValue !== this.props.inputValue) {
console.log('Rerendered!');
}
}
}
Available answers
React handles rerendering automatically based on changes in state or props. While you can observe when it happens, directly controlling the number of rerenders is not a typical pattern in React.
Question 4/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
Which hook is used to set up initial state in a TypeScript functional component?
const [count, setCount] = useState<number>(0);
Available answers
The
useState
hook is used to set up local state in a functional component. You can specify the type using generics, such as useState<number>
.
Question 5/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
In TypeScript, what is a Discriminated Union and how is it useful in React?
type LoadingState = {
state: 'loading';
};
type SuccessState = {
state: 'success';
response: string;
};
type ErrorState = {
state: 'error';
error: Error;
};
interface StateContainer {
stateObject: LoadingState | SuccessState | ErrorState;
}
const Component: React.FC<StateContainer> = ({ stateObject }) => {
switch (stateObject.state) {
case 'loading':
return <div>Loading...</div>;
case 'success':
return <div>{stateObject.response}</div>;
case 'error':
return <div>Error: {stateObject.error.message}</div>;
default:
return null;
}
};
Available answers
Discriminated Unions in TypeScript are a form of union types that allow complex object typing based on a shared, common property (the discriminant) that acts as an identifier—very useful in component prop interfaces for handling multiple types with known distinctions.
Question 6/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
In a TypeScript React component, how can you specify that a prop is optional?
interface MyComponentProps {
name?: string;
}
const MyComponent: React.FC<MyComponentProps> = ({ name }) => {
return <div>{name}</div>;
};
Available answers
In TypeScript, you can make a prop optional by adding
?
after the prop name in the interface.
Question 7/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
How do you define default props in a React component using TypeScript?
interface MyComponentProps {
name?: string;
}
const MyComponent = ({ name = "Guest" }: MyComponentProps) => {
return <div>Hello, {name}</div>;
};
Available answers
In TypeScript, you can set default values for props by using default parameter syntax during destructuring of the props argument.
Question 8/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
Which TypeScript utility can you use to define only some of the properties of an interface as required?
interface Person {
name: string;
age: number;
address?: string;
}
const person: Required<Pick<Person, 'name' | 'age'>> = { name: 'Alice', age: 30 };
Available answers
The
Pick
utility allows you to select a subset of properties, and the Required
utility enforces the selected properties to be required.
Question 9/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What is the `key` prop used for in React lists?
const items = ['a', 'b', 'c'];
const listItems = items.map((item, index) => (
<li key={index}>{item}</li>
));
Available answers
The
key
prop is crucial for React's list rendering because it helps React identify which items have changed, added, or removed, improving the efficiency of the re-rendering process.
Question 10/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
Given the following TypeScript code, what type does the `setColor` function accept?
const [color, setColor] = useState<string | null>(null);
Available answers
The
useState<string | null>
type definition indicates that the state variable and the setter can accept both string and null values.
Question 11/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
How do you type a component that takes a prop function with a return type of `Promise<void>` using TypeScript with React?
interface AsyncComponentProps {
fetchData: () => Promise<void>;
}
const AsyncComponent: React.FC<AsyncComponentProps> = ({ fetchData }) => {
useEffect(() => {
fetchData();
}, [fetchData]);
return <div>Data has been fetched.</div>;
};
Available answers
In this context, the prop is typed using a specific function signature
() => Promise<void>
to indicate it returns a promise resolving void.
Question 12/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
How can you ensure a component in TypeScript only accepts props of a specific shape using TypeScript?
interface ButtonProps {
label: string;
onClick: () => void;
}
const Button: React.FC<ButtonProps> = ({ label, onClick }) => (
<button onClick={onClick}>{label}</button>
);
Available answers
In TypeScript, interfaces or types are used to explicitly define the shape of props a component accepts, ensuring type safety and auto-completion.
Question 13/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
What is the purpose of the `React.FC` type in a TypeScript React component?
const MyComponent: React.FC = () => {
return <div>Hello, World!</div>;
};
Available answers
React.FC
is a type alias provided by React for defining function components with props in TypeScript.
Question 14/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
How can you define and use a custom hook with TypeScript in a React component?
function useCustomHook() {
const [value, setValue] = useState<string>("");
return { value, setValue };
}
const MyComponent: React.FC = () => {
const { value, setValue } = useCustomHook();
return <input value={value} onChange={e => setValue(e.target.value)} />;
};
Available answers
A custom hook in TypeScript is defined as a regular function that may use other hooks internally, and you specify the types as needed.
Question 15/15
😊 Your
answer was correct
🙁 Your
answer was incorrect
How do you define a prop that accepts children in TypeScript with React?
interface MyComponentProps {
children?: React.ReactNode;
}
const MyComponent: React.FC<MyComponentProps> = ({ children }) => {
return <div>{children}</div>;
};
Available answers
In React with TypeScript, the
React.ReactNode
type is used to type the children
prop, as it represents anything that can be rendered in a React component.