TypeScript says click() is missing because the value is typed Element
Property '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().
Fix it fast
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 --noEmit2Match what you see, then jump to its fix
- 1
const el = document.querySelector('#save') then el.click() failsquerySelector with an id or class selector returns ElementFix - 2
The 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
You are about to write as any or as HTMLButtonElement to silence the errorA type assertion would hide the real typeFix - 4
event.target.click() fails inside onClick or addEventListenerIn a React handler, the target is not the element you attached it toFix
- 1
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(" src2Fix
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 --noEmitConfirmed by typescriptlang.org
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" src2Fix
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 --noEmitConfirmed by developer.mozilla.org
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" src2Fix
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 publicConfirmed by typescriptlang.org
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" src2Fix
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 --noEmitConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb 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 fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix