最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 {

发布评论

评论列表(0)

  1. 暂无评论