{"id":"0c72a4629340f316","repo":"sindresorhus/is","slug":"expected-value-which-is-htmlelement-received-va","errorCode":null,"errorMessage":"Expected value which is `HTMLElement`, received value of type `${is(value)}`.","messagePattern":"Expected value which is `HTMLElement`, received value of type `(.+?)`\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"source/index.ts","lineNumber":1659,"sourceCode":"\t\tthrow new TypeError(message ?? typeErrorMessage('Function', value));\n\t}\n}\n\nexport function assertGenerator(value: unknown, message?: string): asserts value is Generator {\n\tif (!isGenerator(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Generator', value));\n\t}\n}\n\nexport function assertGeneratorFunction(value: unknown, message?: string): asserts value is GeneratorFunction {\n\tif (!isGeneratorFunction(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('GeneratorFunction', value));\n\t}\n}\n\nexport function assertHtmlElement(value: unknown, message?: string): asserts value is HTMLElement {\n\tif (!isHtmlElement(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('HTMLElement', value));\n\t}\n}\n\nexport function assertInfinite(value: unknown, message?: string): asserts value is number {\n\tif (!isInfinite(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('infinite number', value));\n\t}\n}\n\nexport function assertInRange(value: number, range: number | [number, number], message?: string): asserts value is number {\n\tif (!isInRange(value, range)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('in range', value));\n\t}\n}\n\nexport function assertInt16Array(value: unknown, message?: string): asserts value is Int16Array {\n\tif (!isInt16Array(value)) {\n\t\tthrow new TypeError(message ?? typeErrorMessage('Int16Array', value));","sourceCodeStart":1641,"sourceCodeEnd":1677,"githubUrl":"https://github.com/sindresorhus/is/blob/7821031c66cdeb7256a0feb2d506535f9e84fcaf/source/index.ts#L1641-L1677","documentation":"Thrown by `assertHtmlElement()` when `isHtmlElement()` fails — the value is not an HTMLElement instance. This guards DOM-manipulating code against receiving selectors, nulls, or non-HTML nodes.","triggerScenarios":"`is.assert.htmlElement(value)` with `null` from `document.querySelector()` finding nothing, a CSS selector string, a jQuery-like wrapper, a text/comment node, an SVGElement, or any value in a non-DOM environment (Node, jsdom edge cases, SSR).","commonSituations":"Running DOM code during server-side rendering or in Node tests without jsdom; querying for an element before the DOM is ready so the selector returns null; SVG elements failing because they are SVGElement, not HTMLElement; elements from a detached iframe/realm depending on how the check is implemented.","solutions":["Handle the null case: check the querySelector result exists before asserting, or fix the selector/timing (run after DOMContentLoaded).","Unwrap wrappers: pass `$(el)[0]` or `ref.current`, not the wrapper object.","If SVG elements are valid inputs, broaden the check to `is.element` (Element) instead of `is.htmlElement`.","Guard against SSR: only run the assertion in browser code paths."],"exampleFix":"// before\nconst el = document.querySelector('#app');\nassertHtmlElement(el); // throws if not found → null\n// after\nconst el = document.querySelector('#app');\nif (el === null) throw new Error('#app not found in DOM');\nassertHtmlElement(el);","handlingStrategy":"validation","validationCode":"if (typeof HTMLElement === 'undefined') {\n  throw new Error('HTMLElement check requires a DOM environment');\n}\nif (!(value instanceof HTMLElement)) {\n  throw new TypeError('Expected an HTMLElement');\n}","typeGuard":"function isHTMLElement(value: unknown): value is HTMLElement {\n  return typeof HTMLElement !== 'undefined' && value instanceof HTMLElement;\n}","tryCatchPattern":"try {\n  assert.htmlElement(el);\n} catch (error) {\n  if (error instanceof TypeError) { /* selector missed or ran outside DOM; handle missing element */ }\n  else throw error;\n}","preventionTips":["`document.querySelector` returns null when nothing matches — null-check before asserting","Text nodes, comments, SVG elements, and document itself are not HTMLElements; check nodeType/interface accordingly","In Node/SSR or jsdom-less tests there is no DOM — gate the code path on environment, and remember cross-iframe elements fail instanceof against the parent realm's HTMLElement"],"tags":["dom","browser","ssr","type-assertion"],"analyzedSha":"7821031c66cdeb7256a0feb2d506535f9e84fcaf","analyzedAt":"2026-07-31T18:34:06.268Z","schemaVersion":2}