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
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
Question 3/15
How does TypeScript handle missing props when destructuring in a function component?
interface MyComponentProps {
name: string;
age?: number;
}
const MyComponent: React.FC<MyComponentProps> = ({ name, age = 0 }) => (
<div>{name}, {age}</div>
);
Select your answer
Question 4/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 5/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 6/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 7/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 8/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 9/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 10/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 11/15
Given the following TypeScript code, what type does the `setColor` function accept?
const [color, setColor] = useState<string | null>(null);
Select your answer
Question 12/15
When using a `useReducer` hook in TypeScript, what type parameters should be provided?
type State = {count: number};
type Action = {type: 'increment'} | {type: 'decrement'};
const reducer = (state: State, action: Action): State => {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
return state;
}
};
Select your answer
Question 13/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 14/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 15/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
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
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.
Question 3/15
π Your
answer was correct
π Your
answer was incorrect
How does TypeScript handle missing props when destructuring in a function component?
interface MyComponentProps {
name: string;
age?: number;
}
const MyComponent: React.FC<MyComponentProps> = ({ name, age = 0 }) => (
<div>{name}, {age}</div>
);
Available answers
TypeScript will require a default value in the destructuring if a prop marked as optional is missing, avoiding runtime errors by setting a default value.
Question 4/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 5/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 6/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 7/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 8/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 9/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 10/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 11/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 12/15
π Your
answer was correct
π Your
answer was incorrect
When using a `useReducer` hook in TypeScript, what type parameters should be provided?
type State = {count: number};
type Action = {type: 'increment'} | {type: 'decrement'};
const reducer = (state: State, action: Action): State => {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
return state;
}
};
Available answers
In TypeScript,
useReducer
should be provided with both state and action types to ensure type-safety within the reducer logic.
Question 13/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 14/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 15/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.