Have you installed type definitions for react
and react-dom
packages? You need to add @types/react
and @types/react-dom
packages and set them in the tsconfig.json
file. Keep in mind, package versions needs to be compatible. Also the expected parameter type of the createRoot
method is Element | DocumentFragment
so you can either use exclamation mark or type assertion like as Element
.
index.tsx
import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
const container = document.getElementById("root");
const root = createRoot(container!);
root.render(<App />);
tsconfig.json
"types": ["react", "react-dom"]
package.json
"dependencies": {
"react": "18.0.0",
"react-dom": "18.0.0",
"react-scripts": "4.0.3"
},
"devDependencies": {
"@types/react": "18.0.0",
"@types/react-dom": "18.0.0",
"typescript": "4.4.2"
},