Skip to content
Tutorials Center
My learning

Topics

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.

JSON
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "react"
  }
}

Try it

TypeScript What the classic transform produces

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.

TypeScript JSX syntax inside a .ts file

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

JSON
{ "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 jsx is unset, the first .tsx file produces an error asking you to provide it (TS17004: "Cannot use JSX unless the '--jsx' flag is provided").
  • Choose by who compiles. tsc emitting code: react-jsx. A bundler or Babel doing the work: preserve, or react-jsx if the bundler reads the output of tsc.
  • Types come from the runtime package. The JSX namespace is found through jsxImportSource, so @types/react (or Preact's own types) must be installed for elements and props to be checked.
  • Generics in .tsx. <T>(x: T) => x is parsed as JSX; write <T,>(x: T) => x or <T extends unknown>(x: T) => x.
  • .ts has no JSX. If you need no JSX at all, leave the option out and the files stay simple.
  • Fragments. <>...</> works in react-jsx without importing Fragment.