Property 'dataset' does not exist on type 'Element' in TS

avatar

Borislav Hadzhiev

Thu Mar 24 20222 min read

banner

Photo by Jonathan Fox

Property 'dataset' does not exist on type 'Element' in TS #

The error "Property 'dataset' does not exist on type 'Element'" occurs when we try to access the dataset property on an element that has a type of Element. To solve the error, use a type assertion to type the element as HTMLElement before accessing the property.

property dataset not exist type element

This is the index.html file for the examples in this article.

index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> </head> <body> <div id="box" data-bar="foo" data-id="example">Box 1</div> <script src="./src/index.ts"></script> </body> </html>

And here is an example of how the error occurs in the index.ts file.

src/index.ts
// 👇️ const box: Element | null const box = document.querySelector('#box'); if (box != null) { // ⛔️ Property 'dataset' does not exist on type 'Element'.ts(2339) console.log(box.dataset.id); box.dataset.id = 'updated id'; console.log(box.dataset.id); }

The reason we got the error is because the return type of the document.querySelector method is Element | null and the dataset property doesn't exist on the Element type.

To solve the error, use a type assertion to type the element as an HTMLElement.

src/index.ts
const box = document.querySelector('#box') as HTMLElement | null; if (box != null) { console.log(box.dataset.id); // 👉️ "example" box.dataset.id = 'updated id'; console.log(box.dataset.id); // 👉️ "updated id" }

If you used the document.getElementsByClassName method, type the collection as HTMLCollectionOf<HTMLElement>.

src/index.ts
// 👇️ with getElementsByClassName // type as HTMLCollectionOf<HTMLElement> const boxes = document.getElementsByClassName( 'box', ) as HTMLCollectionOf<HTMLElement>; for (let i = 0; i < boxes.length; i++) { console.log(boxes[i].dataset.id); boxes[i].dataset.id = 'updated id'; console.log(boxes[i].dataset.id); }

Type assertions are used when we have information about the type of a value that TypeScript can't know about.

We effectively tell TypeScript that the box variable stores anHTMLElement or a null value and not to worry about it.

We used a union type to specify that the variable could still be null, because if an HTML element with the provided selector does not exist in the DOM, the querySelector() method returns a null value.

We used a simple if statement that serves as a type guard to make sure the box variable doesn't store a null value before accessing its dataset property.

src/index.ts
const box = document.querySelector('#box') as HTMLElement | null; // 👉️ box has type HTMLElement or null here if (box != null) { // 👉️ box has type HTMLElement here console.log(box.dataset.id); // 👉️ "example" box.dataset.id = 'updated id'; console.log(box.dataset.id); // 👉️ "updated id" }
TypeScript knows that the box variable has a type of HTMLElement in the if block and allows us to directly access its dataset property.

It's always a best practice to include null in the type assertion, because the querySelector method would return null if no element with the provided selector was found.

Use the search field on my Home Page to filter through my more than 1,000 articles.