diff --git a/examples/react/basic/src/index.tsx b/examples/react/basic/src/index.tsx
index 59a186be1bb..9ddea366c33 100644
--- a/examples/react/basic/src/index.tsx
+++ b/examples/react/basic/src/index.tsx
@@ -157,5 +157,6 @@ function App() {
)
}
-const rootElement = document.getElementById('root') as HTMLElement
+const rootElement = document.getElementById('root')
+if (!rootElement) throw new Error('Missing #root element')
ReactDOM.createRoot(rootElement).render()
diff --git a/examples/react/rick-morty/src/index.tsx b/examples/react/rick-morty/src/index.tsx
index 5482de0621b..066f7751ea3 100644
--- a/examples/react/rick-morty/src/index.tsx
+++ b/examples/react/rick-morty/src/index.tsx
@@ -1,5 +1,6 @@
import ReactDOM from 'react-dom/client'
import App from './App'
-const rootElement = document.getElementById('root') as HTMLElement
+const rootElement = document.getElementById('root')
+if (!rootElement) throw new Error('Missing #root element')
ReactDOM.createRoot(rootElement).render()
diff --git a/examples/react/simple/src/index.tsx b/examples/react/simple/src/index.tsx
index 6a137cb9e26..ba0332f5e6d 100644
--- a/examples/react/simple/src/index.tsx
+++ b/examples/react/simple/src/index.tsx
@@ -45,5 +45,6 @@ function Example() {
)
}
-const rootElement = document.getElementById('root') as HTMLElement
+const rootElement = document.getElementById('root')
+if (!rootElement) throw new Error('Missing #root element')
ReactDOM.createRoot(rootElement).render()
diff --git a/examples/react/star-wars/src/index.tsx b/examples/react/star-wars/src/index.tsx
index 5482de0621b..066f7751ea3 100644
--- a/examples/react/star-wars/src/index.tsx
+++ b/examples/react/star-wars/src/index.tsx
@@ -1,5 +1,6 @@
import ReactDOM from 'react-dom/client'
import App from './App'
-const rootElement = document.getElementById('root') as HTMLElement
+const rootElement = document.getElementById('root')
+if (!rootElement) throw new Error('Missing #root element')
ReactDOM.createRoot(rootElement).render()