TypeScript Reference ยท tsconfig: Language, Modules and Emit Options
TypeScript jsx option
Reference for the jsx compiler option: preserve, react, react-native, react-jsx and react-jsxdev, what each emits, and the factory and import source settings.
jsx controls what TypeScript does with JSX syntax in .tsx files. JSX is not JavaScript, so something must turn <Button label="Save" /> into ordinary calls. The option decides whether the compiler does that, and which function the calls use.
JSX is only legal in files that end in .tsx. In a plain .ts file the angle brackets are parsed as type assertions and you get syntax errors (the second example below shows one). For a modern React project the right value is almost always react-jsx.
{ "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "react" } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
{ "compilerOptions": { "jsx": "react-jsx" } }
| Value | Output |
|---|---|
preserve |
JSX is kept as is and the file is emitted as .jsx. Another tool (Babel, a bundler) compiles it. |
react |
The classic transform: React.createElement(...) calls. React must be in scope. Customize with jsxFactory and jsxFragmentFactory. |
react-jsx |
The automatic runtime: calls to jsx() and jsxs() imported from <jsxImportSource>/jsx-runtime. No React import needed. |
react-jsxdev |
Like react-jsx but uses jsxDEV from jsx-dev-runtime, with file and line information for warnings. |
react-native |
JSX is kept but the file is emitted as .js. |
Related settings: jsxImportSource (default react) picks the package for the automatic runtimes, for example preact. A per-file comment /** @jsxImportSource preact */ overrides it. jsxFactory and jsxFragmentFactory apply only to react.
Usage notes
- The default is nothing. If
jsxis unset, the first.tsxfile produces an error asking you to provide it (TS17004: "Cannot use JSX unless the '--jsx' flag is provided"). - Choose by who compiles.
tscemitting code:react-jsx. A bundler or Babel doing the work:preserve, orreact-jsxif the bundler reads the output oftsc. - Types come from the runtime package. The
JSXnamespace is found throughjsxImportSource, so@types/react(or Preact's own types) must be installed for elements and props to be checked. - Generics in
.tsx.<T>(x: T) => xis parsed as JSX; write<T,>(x: T) => xor<T extends unknown>(x: T) => x. .tshas no JSX. If you need no JSX at all, leave the option out and the files stay simple.- Fragments.
<>...</>works inreact-jsxwithout importingFragment.