React error Argument type 'HTMLElement or null' not assignable to parameter type 'Element or Document
Use non-null assertions or type assertions to resolve the React.js error "Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element | DocumentFragment'", for example const root = createRoot(rootElement!)
.
The following is an example that produces this error
import App from './App';
import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';
const rootElement = document.getElementById('root');
// 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)
const root = createRoot(rootElement);
root.render(
<StrictMode>
<App />
</StrictMode>,
);
The problem here is the return type of document.getElementById method is HTMLElement | null
.
If the provided id does not exist in the DOM, the method returns null.
On the other hand, the expected parameter type of the createRoot method is Element | DocumentFragment, so the provided parameter type does not match the expected parameter type.
One way to fix the error is to use the non-null (!)
assertion operator.
import App from './App';
import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';
const rootElement = document.getElementById('root');
// non-null (!) assertion
const root = createRoot(rootElement!);
root.render(
<StrictMode>
<App />
</StrictMode>,
);
The not-null (!) assertion operator removes null and undefined from a type without doing any explicit type checking.
When you use this approach, you are basically telling TypeScript that the rootElement variable will never be null or undefined. Therefore, rootElement
the type of the variable becomes % HTMLElement
instead of % HTMLElement | null
.
Alternatively, we can use simple type assertions.
import App from './App';
import {StrictMode} from 'react';
import {createRoot} from 'react-dom/client';
const rootElement = document.getElementById('root');
// 使用类型断言
const root = createRoot(rootElement as Element);
root.render(
<StrictMode>
<App />
</StrictMode>,
);
Type assertions are used when we have information about the type of a value that TypeScript cannot know about.
We are actually telling TypeScript that the rootElement variable stores a value of type Element and that we don’t need to worry about it.
We determined the correct type from the error message: "Argument of type 'HTMLElement | null' is not assignable to parameter of type 'Element | DocumentFragment'".
With this error message, TypeScript is telling us: the expected parameter type of the function is Element | DocumentFragment, but you called the function with an argument of type HTMLElement | null.
The types are incompatible because the parameter type may be null.
To fix this error, we must make the passed in argument type compatible with the expected argument type.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to avoid cross-origin (CORS) issues in React/Next.js
Publish Date:2025/03/17 Views:170 Category:NETWORK
-
In this article, we will introduce how to avoid cross-origin (CORS) issues in React/Next.js. Cross-origin resource sharing (CORS) is a protocol that defines how web requests should be handled when crossing different URLs.
React Tutorial - Transferring Props
Publish Date:2025/03/16 Views:188 Category:React
-
React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.
React Tutorial: Props Anti-Pattern
Publish Date:2025/03/16 Views:187 Category:React
-
React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.
React Tutorial - Props Validation
Publish Date:2025/03/16 Views:102 Category:React
-
Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.
React tutorial: Types of Props for child components
Publish Date:2025/03/16 Views:172 Category:React
-
Usually, the child components of a React component are a group, that is, the child components are an array. Introduction to Type of the Children Props.
How to solve the error Uncaught TypeError: Cannot read properties of undefined in
Publish Date:2025/03/16 Views:153 Category:React
-
In the process of React development, we often encounter some errors. Here we look at an error reported in App.js. The error is as follows: App.js:69 Uncaught TypeError: Cannot read properties of undefined (reading 'setState') at onInput
Why do you need to bind event handlers in React Class Components?
Publish Date:2025/03/16 Views:60 Category:React
-
When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:
Solution to the error "does not contain a default export" in React
Publish Date:2025/03/16 Views:191 Category:React
-
When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.
Error in React: Attempted import error 'X' is not exported from Solution
Publish Date:2025/03/16 Views:78 Category:React
-
In React, the error “Attempted import error 'X' is not exported from” in React.js occurs when we try to import a named import that does not exist in the specified file. To fix the error, make sure the module has named exports and you have not obfu