Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 708
Dumitru Birsan Avatar asked Oct 23 '25 11:10

Dumitru Birsan


1 Answers

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

const { list } = /**@type {HTMLInputElement}*/(document.getElementById(someInput));
like image 154
maksimr Avatar answered Oct 25 '25 00:10

maksimr