Home

Published

- 1 min read

how to use typescript on createRoot

img of how to use typescript on createRoot

The solution for this is noted below

how to use typescript on createRoot

Solution

   // Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element | DocumentFragment'.
// Type 'null' is not assignable to type 'Element | DocumentFragment'.ts(2345)

// typescript error on createRoot (React 18)

// const root = ReactDOM.createRoot(document.getElementById("root"))


--- add non-null assertion

const root = ReactDOM.createRoot(document.getElementById("root")!)

Try other methods by searching on the site. That is if this doesn’t work