Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript says click() is missing because the value is typed Element

ErrorProperty 'click' does not exist on type 'Element'.

Short answer

click() is defined on HTMLElement, not on its parent type Element. querySelector with an id or class selector returns Element, so the call fails. Pass the element type as a generic, such as querySelector<HTMLButtonElement>('#save'), or narrow with instanceof before calling click().

  • Applies to TypeScript 5.5, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Hover the variable in your editor, or print the type with tsc, to see whether it is Element, EventTarget or an HTMLElement type.

    npx tsc --noEmit
  2. 2Match what you see, then jump to its fix

    1. 1const el = document.querySelector('#save') then el.click() failsquerySelector with an id or class selector returns ElementFix
    2. 2The element comes from a function that returns Element or Node, and you cannot name its exact typeThe value is not known to be an HTML element at allFix
    3. 3You are about to write as any or as HTMLButtonElement to silence the errorA type assertion would hide the real typeFix
    4. 4event.target.click() fails inside onClick or addEventListenerIn a React handler, the target is not the element you attached it toFix

What does the error look like?

  • Property 'click' does not exist on type 'Element'.

Cause 1 of 4

querySelector with an id or class selector returns Element

1Check

TypeScript has overloads for querySelector. A plain tag name such as 'button' returns the matching element type. Any other string falls to the last overload, where the type defaults to Element.

grep -rn "querySelector(" src

2Fix

Name the element type as a generic argument and handle null, because querySelector returns null when nothing matches.

const btn = document.querySelector<HTMLButtonElement>('#save');
btn?.click();

3Verify

Run the compiler again. The error is gone.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 4

The value is not known to be an HTML element at all

1Check

HTMLElement extends Element. click() lives on HTMLElement, so a plain Element has no such method.

grep -rn "as Element\|: Element" src

2Fix

Narrow at run time with instanceof. Unlike an as cast, it also protects you when the value is a different element.

const el = document.querySelector('#save');
if (el instanceof HTMLElement) {
  el.click();
}

3Verify

Compile again, then run the code path in the browser.

npx tsc --noEmit

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

A type assertion would hide the real type

1Check

A type assertion is erased at compile time and does not check anything. If the selector matches a link and you assert a button, click() still compiles.

grep -rn " as HTMLButtonElement" src

2Fix

Use an assertion only when the markup guarantees the type. In every other case use the generic form or instanceof from the causes above.

// Only when you know the markup
const btn = document.getElementById('save') as HTMLButtonElement;

3Verify

Confirm the id exists on a button in the page markup.

grep -rn 'id="save"' src public

Confirmed by typescriptlang.org

Did this fix it?

Cause 4 of 4

In a React handler, the target is not the element you attached it to

1Check

event.target is where the event started, which can be a child. event.currentTarget is the element the handler is attached to. React types currentTarget from the event's generic argument.

grep -rn "event.target\|e.target" src

2Fix

Type the handler with the element and read currentTarget.

function handleClick(event: React.MouseEvent<HTMLButtonElement>) {
  event.currentTarget.click();
}

3Verify

Compile, then click the button in the app.

npx tsc --noEmit

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Give querySelector the element type every time you use an id or class selector.
  • Prefer instanceof over as when the DOM comes from outside your control.
  • Treat every querySelector result as possibly null and check it before use.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.