Argument type ‘HTMLElement or null’ not assignable to parameter type ‘Element or DocumentFragment’ – How to fix it?

Argument type ‘HTMLElement or null’ not assignable to parameter type ‘Element or DocumentFragment’ – How to fix it?

In this article, we’ll talk about the error “Argument type ‘HTMLElement or null’ not assignable to parameter type ‘Element or DocumentFragment'” in React. To fix it you can use the non-null assertion or type assertion. Let’s see what it is and figure out the most effective solution. 

The cause of error “Argument type ‘HTMLElement or null’ not assignable to parameter type ‘Element or DocumentFragment'”

You get the “Argument type ‘HTMLElement or null’ not assignable to parameter type ‘Element or DocumentFragment” error because there is an asynchrony between the type of the result returned by document.getElementById() method and the parameter of createRoot() method.

The document.getElementById() returns HTMLElement or null value. Otherwise, createRoot() requires an input parameter of Element or DocumentFragment.

To illustrate, here is an example:

import App from './App';
import React from 'react';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root'));

root.render(<App />);

Error:

Copy the above code to your computer, you will see this error in the line: const root = createRoot(document.getElementById('root'));

Solutions for this error

I will show you two ways to fix this problem quickly and easily. Let’s see what those ways are.

Use the non-null assertion (“!”)

The first method introduced here is to use the non-null assertion (“!“).

The non-null assertion removes null and undefined from a specified type without actually performing a specific type check.

Apply to the original example to fix the error:

import App from './App';
import React from 'react';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root')!);
                        
root.render(<App />);

By using the non-null assertion, now the return value of method document.getElementById() will only be HTMLElement instead of HTMLElement | null.

Use the type assertion

Another approach for you, use the type assertion (as keyword) to tell TypeScript about the type of a particular value that TypeScript doesn’t already know.

Apply to the original example to fix the error:

import App from './App';
import React from 'react';
import { createRoot } from 'react-dom/client';

const root = createRoot(document.getElementById('root') as Element);

root.render(<App />);

Take a look at the error message again. We know that the parameter type of the function must be Element | DocumentFragment.

With type assertion, TypeScript will be informed that our root object will be of type Element. It doesn’t need to worry about null values ​​or some values that will create incompatibility between the passed-in argument and the required parameter type of the method.

Summary

The “Argument type ‘HTMLElement or null’ not assignable to parameter type ‘Element or DocumentFragment'” error has been solved. I hope the information in this article was useful to you. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *