Sub topic 1
Setting up TypeScript for TSX
TSX is TypeScript with JSX syntax. A TSX file ends in .tsx and lets you write React markup while the compiler checks the types of everything in it. This chapter configures a project so that checking is strict and works without extra imports. Later chapters assume this setup.
Prerequisites
This chapter assumes you know the following:
- Basic React: function components, props, state, and JSX.
- TypeScript fundamentals: interfaces, type aliases, unions, and generics.
- How to run a TypeScript project with a bundler or framework such as Vite or Next.js.
You also need Node.js, and the typescript, react, react-dom, @types/react, and @types/react-dom packages installed.
What the compiler does with TSX
The TypeScript compiler (tsc) reads each .tsx file and checks that every JSX element matches the types of the component it uses. For example, it reports an error if you pass a number to a prop that expects a string.
In most projects, your bundler or framework produces the final JavaScript. TypeScript only checks types. This is why the configuration below turns off emitting files.
A minimal tsconfig.json
Create a tsconfig.json in the project root:
{
"compilerOptions": {
"strict": true,
"jsx": "react-jsx",
"module": "esnext",
"moduleResolution": "bundler",
"noEmit": true
}
}Each option does one job:
| Option | Value | Effect |
|---|---|---|
| `strict` | `true` | Turns on the strict family of checks, including `strictNullChecks` and `noImplicitAny`. |
| `jsx` | `"react-jsx"` | Uses the automatic JSX runtime, so you do not import React in every file. |
| `module` | `"esnext"` | Keeps modern `import` and `export` syntax for the bundler to handle. |
| `moduleResolution` | `"bundler"` | Resolves imports the way Vite, webpack, and similar tools do. |
| `noEmit` | `true` | Checks types only. The bundler writes the output. |
Why strict matters for React
Several patterns in this tutorial depend on strict mode. With strictNullChecks, a ref's current value can be null, and the compiler forces you to handle that. State that starts as null must be typed as a union. Without strict, these mistakes pass silently and fail at runtime.
Why the JSX setting matters
The jsx option controls how TypeScript treats angle-bracket markup. If it is missing, .tsx files fail with an error such as "Cannot use JSX unless the '--jsx' flag is provided". With the older "react" value, every file must contain import React from "react". The "react-jsx" value avoids that.
Verify the setup
Add a deliberate mistake to a component and check that the compiler catches it:
function Greeting({ name }: { name: string }) {
return <p>Hello, {name}</p>;
}
export function App() {
return <Greeting name={42} />;
}Run the checker from the terminal:
npx tsc --noEmitYou should see an error like Type 'number' is not assignable to type 'string' on the name prop. Change 42 to "Ada" and the error disappears.
Common errors
- Missing React types. If you see "Could not find a declaration file for module 'react'", install
@types/react. - Wrong file extension. JSX in a
.tsfile is a syntax error. Rename the file to.tsx. - Old resolution mode.
"moduleResolution": "bundler"requires TypeScript 5.0 or later. On older versions, use"node". - Editor disagrees with the terminal. Make sure your editor uses the workspace TypeScript version, not a bundled one.
Exercise
Your teammate's .tsx files fail with "Cannot use JSX unless the '--jsx' flag is provided". Which compiler option is missing, and which value lets you skip importing React in every file?
Show answer
The jsx option is missing. Setting it to "react-jsx" enables the automatic JSX runtime, so files do not need import React from "react".
Exercise
Why is noEmit set to true in a project that uses Vite?
Show answer
Vite builds the JavaScript output itself. TypeScript is used only to check types, so it does not need to write files.