Search code examples
javascriptjsdoc

Jsdoc casting htmlElements to other types without typescript


I've been trying to use jsdoc with ts-check for error check/autocomplete/intellisense, the only issue i've been having are selections (which i cannot cast as i do not use typescript).

when i try to set the type of an element it will instead underline it and display the following error

Type 'HTMLElement | null' is not assignable to type 'HTMLInputElement'

// @ts-check

/**
 * @type {HTMLInputElement}
 */
const { list } = document.getElementById(someInput)

if i remove ts-check it will remove the error and offer intellisense but it will not show errors anymore.

I have been looking for solutions to this issue but they are all for typescript .


Solution

  • /**@type {🔥}*/(expression) is an equivalent of type cast as operator from TypeScript

    const { list } = /**@type {HTMLInputElement}*/(document.getElementById(someInput));