JavaScript and React course ยท Module 12: React and TypeScript
Typing Functional Components
In this lesson3
You built a ship module, connected it to the rest, and a week later a crew member passes it a number where it expected text. Nobody noticed, because the component had no operating manual. In React, every component is a function. TypeScript lets us precisely define what that function accepts (props) and what it returns (JSX). It's like an operating manual for every module on a spaceship.
React.FC vs Explicit Typing
There are two popular ways to type functional components in React. You'll meet both in other people's code, so it's worth understanding each before choosing yours.
Approach 1: React.FC (Function Component)
React.FC (short for React.FunctionComponent) is a generic type: you pass the props type in angle brackets and it describes the whole component function. You attach it to the constant, not to the parameter. To use the name React.FC you need to import React (or just the FC type).
import React from 'react';
// React.FC describes the whole component function (props + return type)
const StarInfo: React.FC = () => {
return <p>Star information</p>;
};Without a generic parameter, React.FC means a component with no props. When props are needed, we first describe them with a StarProps interface and then pass that interface in the angle brackets:
// React.FC with props
interface StarProps {
name: string;
brightness: number;
}
const StarCard: React.FC<StarProps> = ({ name, brightness }) => {
return (
<div>
<h3>{name}</h3>
<p>Brightness: {brightness}</p>
</div>
);
};TypeScript now knows that name is a string and brightness a number, even though the destructuring itself has no types - they came from the annotation on the StarCard constant.
Approach 2: Explicit Typing (Recommended)
The second approach types the function parameter itself, exactly like any ordinary TypeScript function. You can write the return type as React.JSX.Element, meaning "a JSX element".
// Explicit typing - more transparent and flexible
interface StarProps {
name: string;
brightness: number;
}
function StarCard({ name, brightness }: StarProps): React.JSX.Element {
return (
<div>
<h3>{name}</h3>
<p>Brightness: {brightness}</p>
</div>
);
}The same notation works in an arrow function - the props type still sits on the parameter, not on the constant:
// Or as an arrow function
const StarCardArrow = ({ name, brightness }: StarProps): React.JSX.Element => {
return (
<div>
<h3>{name}</h3>
<p>Brightness: {brightness}</p>
</div>
);
};The JSX inside didn't change by a single character compared with approach 1. The only difference is where the type information lives.
Why is Explicit Typing Recommended?
The React community has largely moved away from React.FC for several reasons:
- React.FC used to add children - up to @types/react 17, every React.FC component "accepted"
children, even when it didn't use them. Since @types/react 18 that is no longer true, but many old articles still repeat it - Explicit typing is more readable - you can see exactly what the component accepts
- Better compatibility - it works identically with function and arrow function, and with generic components, which React.FC can't express
See what happens when you try to use children in both approaches in modern React:
// React.FC - since @types/react 18 children is NOT added automatically
const Badge: React.FC<{ label: string }> = ({ label, children }) => {
return <span>{label}{children}</span>; // ERROR: Property 'children' does not exist
};
// Explicit typing - children must be explicitly added
interface BadgeProps {
label: string;
}
function Badge({ label }: BadgeProps): React.JSX.Element {
return <span>{label}</span>; // no children, because it's not in props
}In both cases, if a component should accept children, you must add them to the props type - we'll do that in the next lesson. My advice: write explicit types on the parameter. It's shorter, clearer and matches the examples on react.dev.
Component Return Type
A React component can return different things: a JSX element, text, a number, or null when there's nothing to show. Each option has its own type:
// React.JSX.Element - most commonly used
function Planet(): React.JSX.Element {
return <div>Mars</div>;
}
// React.ReactElement - more general React element type
function Planet(): React.ReactElement {
return <div>Mars</div>;
}An important correction to older tutorials: writing JSX.Element without a prefix relied on the global JSX namespace, which was removed from the React 19 types. Write React.JSX.Element - it works in React 18 and 19. If a component can return null or text, use the widest type:
// React.ReactNode - most flexible (JSX, string, number, null)
function MaybeContent(): React.ReactNode {
return null; // can return null
}
// In practice, we most often omit the return type
// TypeScript infers it automatically
function Planet() {
return <div>Mars</div>; // TypeScript knows this is React.JSX.Element
}A component returning React.ReactNode can be used in JSX since TypeScript 5.1. In daily work you usually just omit the return type and let the compiler infer it - typed props are the part that truly protects your ship.
Practice both styles in the editor. In the next lesson we'll extend the props interface with optional fields and children.
Code for this lesson: App.tsx
import React from 'react';
// Props interface
interface CrewMemberProps {
name: string;
rank: string;
specialization: string;
yearsInSpace: number;
}
// Method 1: React.FC (Function Component)
const CrewBadgeFC: React.FC<CrewMemberProps> = ({ name, rank, specialization, yearsInSpace }) => {
return (
<div className="badge-card">
<div className="badge-header">FC</div>
<h3>{name}</h3>
<p className="rank">{rank}</p>
<p>Specialization: {specialization}</p>
<p>Years in space: {yearsInSpace}</p>
</div>
);
};
// Method 2: Explicit typing (recommended)
function CrewBadgeExplicit({ name, rank, specialization, yearsInSpace }: CrewMemberProps): JSX.Element {
return (
<div className="badge-card">
<div className="badge-header">Explicit</div>
<h3>{name}</h3>
<p className="rank">{rank}</p>
<p>Specialization: {specialization}</p>
<p>Years in space: {yearsInSpace}</p>
</div>
);
}
// Method 3: Arrow function with an explicit type
const CrewBadgeArrow = ({ name, rank, specialization, yearsInSpace }: CrewMemberProps): JSX.Element => {
return (
<div className="badge-card">
<div className="badge-header">Arrow</div>
<h3>{name}</h3>
<p className="rank">{rank}</p>
<p>Specialization: {specialization}</p>
<p>Years in space: {yearsInSpace}</p>
</div>
);
};
function App() {
const member: CrewMemberProps = {
name: "Nova",
rank: "Captain",
specialization: "Navigation",
yearsInSpace: 15
};
return (
<div className="app">
<h1>Ways to type components</h1>
<div className="cards">
<CrewBadgeFC {...member} />
<CrewBadgeExplicit {...member} />
<CrewBadgeArrow {...member} />
</div>
<p className="note">All three approaches give the same result. The explicit typing approach (Explicit) is recommended.</p>
</div>
);
}
export default App;Remember: the props type is the module's operating manual - stick it to the parameter and every crew member knows how to plug it in.
Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What file extension is used for React components written in TypeScript?
2. Which approach to typing functional components is currently recommended by the React community?
Hands-on tasks in the game
- Code editor
Basic typing of interfaces and components
- Click in order
Arrange the syntax of an interface definition in TypeScript: