Understanding TSX
Understanding TSX — purpose and prerequisites
TSX allows JSX expressions inside TypeScript source. Capitalized component tags refer to component values; lowercase tags describe intrinsic elements. JSX checks use configured React and DOM types, while the configured JSX transform determines emitted JavaScript. Do not confuse a visual tree with an HTML string.
What you’ll learn
- Explain the purpose of understanding tsx.
- Identify the typed contract, runtime responsibilities and common pitfalls.
- Run the example and extend its behavior with a practical exercise.
How the idea works
This standalone function demonstrates the data transformation. The companion .tsx file returns an actual React element and illustrates the jsx:react-jsx compiler setting.
A TSX component expression is not executable in plain Node.js unless JSX has been compiled and its runtime is available.
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 Badge = { label: string; count: number };
function badgeText(props: Badge): string {
return `${props.label}: ${props.count}`;
}
console.log(badgeText({ label: "Tasks", count: 4 }));Verified output
Tasks: 4Compile: npx tsc --strict --target es2022 --module commonjs tsx.ts; run: node tsx.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
This standalone function demonstrates the data transformation. The companion .tsx file returns an actual React element and illustrates the jsx:react-jsx compiler setting.
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 component expression is not executable in plain Node.js unless JSX has been compiled and its runtime is available.
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
Write a List component rendering three list items with stable keys and typed props.
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
Additional official reference
Review the official documentation for the provider, framework or runtime, especially when installing a newer major version.
Key idea to remember
This standalone function demonstrates the data transformation. The companion .tsx file returns an actual React element and illustrates the jsx:react-jsx compiler setting.
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.