Introduction to React with TypeScript
Introduction to React with TypeScript — purpose and prerequisites
React builds interfaces from components; TypeScript checks props, state types, event handlers, and component-return types. JSX/TSX transforms into JavaScript calls during a build step. A compiler cannot create React components at runtime unless React and a suitable render environment are installed.
What you’ll learn
- Explain the purpose of introduction to react with typescript.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
The pure prop-to-view calculation models one concern within a React component. A real TSX companion uses these props inside JSX with React installed and a browser build.
A .tsx file does not run as browser JavaScript without transforming JSX and supplying its runtime libraries.
Visualize the flow
Run the checked TypeScript example
This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.
TypeScript source
type WelcomeProps = Readonly<{ name: string; unread: number }>;
function welcomeText(props: WelcomeProps): string {
return `Hello ${props.name} (${props.unread} unread)`;
}
console.log(welcomeText({ name: "Anika", unread: 3 }));Verified output
Hello Anika (3 unread)Compile: npx tsc --strict --target es2022 --module commonjs react.ts; run: node react.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.
Trace the execution
The pure prop-to-view calculation models one concern within a React component. A real TSX companion uses these props inside JSX with React installed and a browser build.
Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.
Build the practical integration
Integration companion: The primary example above is self-contained. The files below demonstrate a real framework, runtime, or project setup and may need additional packages or a browser.
src/Tasks.tsx
import { useState, type FormEvent } from "react";
type Todo = Readonly<{ id: number; title: string; done: boolean }>;
export default function Tasks() {
const [tasks, setTasks] = useState<Todo[]>([]);
const [title, setTitle] = useState("");
function add(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
if (!title.trim()) return;
setTasks(current => [...current, { id: Date.now(), title: title.trim(), done: false }]);
setTitle("");
}
return <section><h1>Tasks</h1><form onSubmit={add}>
<input aria-label="Task title" value={title} onChange={e => setTitle(e.currentTarget.value)} />
<button>Add</button></form>
<ul>{tasks.map(task => <li key={task.id}><label>
<input type="checkbox" checked={task.done} onChange={() => setTasks(current =>
current.map(item => item.id === task.id ? { ...item, done: !item.done } : item))} />
{task.title}</label></li>)}</ul>
</section>;
}Setup and execution
npm create vite@latest typed-tasks -- --template react-ts
cd typed-tasks
Edit src/App.tsx with the component shown above
npm install
npm run dev
npm run buildThis is a real React TSX component, not a Node-only simulation. Use the React+TypeScript Vite template, and place the component in the application's src/App.tsx. Its imports require React packages and @types/react; browser behavior was not executed in this environment.
Validation: This companion requires the named external packages or browser environment. Its runtime behavior was not executed during the course build.
Common mistake and correction
A .tsx file does not run as browser JavaScript without transforming JSX and supplying its runtime libraries.
Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.
Your turn: extend the example
Create a Welcome component with typed name and unread props. Show it twice with different values.
Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.
Check your understanding
Key idea to remember
The pure prop-to-view calculation models one concern within a React component. A real TSX companion uses these props inside JSX with React installed and a browser build.
Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.
Reaching the recap and completing the quizzes are separate milestones.