Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript reads a React context provider as a type, not as JSX

ErrorCannot find namespace 'ctx'

Short answer

TypeScript only parses JSX in files named .tsx, and only when the jsx compiler option is set. In a .ts file the JSX is not parsed as JSX, which is how a name like ctx ends up reported as a missing namespace. Rename the file to .tsx and set jsx to react-jsx in tsconfig.json.

  • Applies to TypeScript 5.x, React 19, Vite current
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Look at the extension of the file that holds the JSX, and at the jsx line in tsconfig.json.

    ls src/context/
    grep -n "\"jsx\"" tsconfig*.json
  2. 2Match what you see, then jump to its fix

    1. 1The file has a return like <ctx.Provider value={...}>, and its name ends in .tsThe file that contains JSX is named .tsFix
    2. 2The file is already .tsx, and the error remainsThe jsx compiler option is not set in tsconfig.jsonFix

What does the error look like?

  • Cannot find namespace 'ctx'
  • the same context code compiles when the file is named .tsx

Cause 1 of 2

The file that contains JSX is named .ts

1Check

The TypeScript handbook gives two requirements for JSX: name the file with a .tsx extension, and enable the jsx option. A .ts file does not get JSX parsing, so the markup around ctx.Provider is not understood.

ls src/context/*.ts | xargs grep -ln "<.*Provider"

2Fix

Rename the file to .tsx and update the imports if they spell the extension.

git mv src/context/AppContext.ts src/context/AppContext.tsx

3Verify

Restart the type checker. The namespace error is gone.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 2

The jsx compiler option is not set in tsconfig.json

1Check

Without the jsx option, TypeScript refuses JSX even in .tsx files. The tsconfig reference lists the values preserve, react, react-native, react-jsx and react-jsxdev.

grep -n "\"jsx\"" tsconfig.json tsconfig.app.json 2>/dev/null

2Fix

Set jsx in the compilerOptions that apply to your source files. The react-jsx value uses the automatic runtime, so you do not need to import React in each file.

{
  "compilerOptions": {
    "jsx": "react-jsx"
  }
}

3Verify

Run the type checker again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Name every file that contains JSX with .tsx, and keep plain logic in .ts.
  • Use as for type assertions: the handbook says angle bracket assertions are not allowed in .tsx files.
  • Create new React projects from a current template, which sets jsx for you.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.