-
-
Save rbiggs/45d3b59a72c1965c551ece398c67ce1e to your computer and use it in GitHub Desktop.
Example of JSDoc type casting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a button element. | |
// Type will be Node. | |
const btn = document.createElement('button') | |
// Because type is Node, we cannot call setAttribute, | |
// since this is on the Element type. | |
// Performing a type cast from Node to Element fixes this: | |
/** @type {Element} */(btn).setAttribute('disabled', true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is unnecessary in this case as the return type of
document.createElement
isElement
, and if you pass a statically known string (eg.:'button'
, then the return value becomesHTMLButtonElement
):https://github.com/Microsoft/TypeScript/blob/a41a27694ac9ca2848fb83cc0a5414c4c2c5338e/lib/lib.dom.d.ts#L17187-L17303