最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Jsdoc casting htmlElements to other types without typescript - Stack Overflow

matteradmin8PV0评论

I've been trying to use jsdoc with ts-check for error check/autoplete/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 .

I've been trying to use jsdoc with ts-check for error check/autoplete/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 .

Share Improve this question asked Dec 22, 2022 at 16:26 Dumitru BirsanDumitru Birsan 8568 silver badges15 bronze badges 2
  • document.getElementById(someInput) returns HTMLElement | null, you may not always be able to de-structure it to get list. If you're using VSCode, it might be worth using ESLint in bination with typescript installed. If you want typescript checking, why not use typescript? – Harrison Commented Dec 22, 2022 at 16:33
  • 2 @Harrison jsdoc + typescript solve most of the problems and remove pilation step which may significantly slowdown development. Also, I like the idea of types in ments because it doesn't allow you to use syntax different from JavaScript (like enums) and clearly separate types from runtime code – maksimr Commented Dec 22, 2022 at 16:56
Add a ment  | 

1 Answer 1

Reset to default 12

/**@type {

Post a comment

comment list (0)

  1. No comments so far