Google Closure Compiler page on the topic<\/a>.<\/p>\n\n\nType casting<\/h3>\n\n\n While strong typing is often very helpful, you may find that TypeScript\u2019s built-in expectations don\u2019t quite work for your use case. In that sort of instance, we might need to cast an object to a new type. One instance of when this might be necessary is when working with event listeners.<\/p>\n\n\n\n
In TypeScript, all event listeners take a function as a callback where the first argument is an object of type Event<\/code>, which has a property, target, that is an EventTarget<\/code>. This is<\/em> the correct type per the DOM standard, but oftentimes the bit of information we want out of the event\u2019s target doesn\u2019t exist on EventTarget<\/code> \u2014 such as the value property that exists on HTMLInputElement.prototype<\/code>. That makes the following code invalid:<\/p>\n\n\n\ndocument.querySelector('input').addEventListener(event => {\n\u00a0 console.log(event.target.value);\n};<\/code><\/pre>\n\n\n\nTypeScript will complain that the property value<\/code> doesn\u2019t exist on EventTarget<\/code> even though we, as developers, know fully well that an <input><\/code> does have a value<\/code>.<\/p>\n\n\n\n <\/figure>\n\n\n\nIn order for us to tell TypeScript that we know event.target<\/code> will be an HTMLInputElement<\/code>, we must cast the object\u2019s type:<\/p>\n\n\n\ndocument.getElementById('input').addEventListener('input', event => {\n\u00a0 console.log(\/** @type {HTMLInputElement} *\/(event.target).value);\n});<\/code><\/pre>\n\n\n\nWrapping event.target<\/code> in parenthesis will set it apart from the call to value<\/code>. Adding the type before the parenthesis will tell TypeScript we mean that the event.target<\/code> is something different than what it ordinarily expects.<\/p>\n\n\n\n <\/figure>\n\n\n\nAnd if a particular object is being problematic, we can always tell TypeScript an object is @type {any}<\/code> to ignore error messages, although this is generally considered bad practice depsite being useful in a pinch.<\/p>\n\n\nWrapping up<\/h3>\n\n\n TypeScript is an incredibly powerful tool that many developers are using to streamline their workflow around consistent code standards. While most applications will utilize the built-in compiler, some projects might decide that the extra syntax that TypeScript provides gets in the way. Or perhaps they just feel more comfortable sticking to standards rather than being tied to an expanded syntax. In those cases, developers can still get the benefits of utilizing TypeScript\u2019s type system even while writing vanilla JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"
Unless you\u2019ve been hiding under a rock the last several years (and let\u2019s face it, hiding under a rock sometimes feels like the right thing to do), you\u2019ve probably heard of and likely used TypeScript. TypeScript is a syntactical superset of JavaScript that adds \u2014 as its name suggests \u2014 typing to the web\u2019s favorite […]<\/p>\n","protected":false},"author":38145,"featured_media":318809,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"c2c_always_allow_admin_comments":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[1259],"jetpack_publicize_connections":[],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/08\/typscript-hollow.png?fit=1200%2C600&ssl=1","jetpack-related-posts":[{"id":362478,"url":"https:\/\/css-tricks.com\/the-relevance-of-typescript-in-2022\/","url_meta":{"origin":317887,"position":0},"title":"The Relevance of TypeScript in 2022","date":"January 31, 2022","format":false,"excerpt":"It\u2019s 2022. And the current relevance of TypeScript is undisputed. TypeScript has dominated the front-end developer experience by many, many accounts. By now you likely already know that TypeScript is a superset of JavaScript, building on JavaScript by adding syntax for type declarations, classes, and other object-oriented features with type-checking.\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/01\/typescript-logo.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":331325,"url":"https:\/\/css-tricks.com\/integrating-typescript-with-svelte\/","url_meta":{"origin":317887,"position":1},"title":"Integrating TypeScript with Svelte","date":"December 24, 2020","format":false,"excerpt":"Svelte is one of the newer JavaScript frameworks and it\u2019s rapidly rising in popularity. It\u2019s a template-based framework, but one which allows for arbitrary JavaScript inside the template bindings; it has a superb reactivity story that\u2019s simple, flexible and effective; and as an ahead-of-time (AOT) compiled framework, it has incredibly\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/12\/typescript-svelte-logos.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":362091,"url":"https:\/\/css-tricks.com\/typescript-discriminated-unions\/","url_meta":{"origin":317887,"position":2},"title":"Demystifying TypeScript Discriminated Unions","date":"January 27, 2022","format":false,"excerpt":"TypeScript is a wonderful tool for writing JavaScript that scales. It\u2019s more or less the de facto standard for the web when it comes to large JavaScript projects. As outstanding as it is, there are some tricky pieces for the unaccustomed. One such area is TypeScript discriminated unions. Specifically, given\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2022\/01\/typscript-discriminated-unions.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":337673,"url":"https:\/\/css-tricks.com\/the-deno-company\/","url_meta":{"origin":317887,"position":3},"title":"The Deno Company","date":"April 2, 2021","format":false,"excerpt":"I'm sure a lot of you are paying attention to Deno anyway, the next-gen JavaScript-on-the-sever project from Node creator Ryan Dahl, especially after dropping all these candid regrets about what happened in Node. But perhaps you're paying more attention now that Deno has taken some seed investment and will be\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2021\/04\/demo.jpg?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":285982,"url":"https:\/\/css-tricks.com\/creating-reusable-base-classes-in-typescript-with-a-real-life-example\/","url_meta":{"origin":317887,"position":4},"title":"Creating Reusable Base Classes in TypeScript with a Real-Life Example","date":"April 18, 2019","format":false,"excerpt":"Hey CSS-Tricksters! Bryan Hughes was kind enough to take a concept from an existing post he published on converting to TypeScript and take it a few couple steps further in this post to elaborate on creating reusable base classes. While this post doesn\u2019t require reading the other one, it\u2019s certainly\u2026","rel":"","context":"In "Article"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2019\/04\/typscript-color-swatch.png?fit=1200%2C600&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":254325,"url":"https:\/\/css-tricks.com\/typescript-at-slack\/","url_meta":{"origin":317887,"position":5},"title":"TypeScript at Slack","date":"April 29, 2017","format":false,"excerpt":"An excellent subhead by Felix Rieseberg: How I Learned to Stop Worrying & Trust the Compiler. I'd wager that some of the popularity of SCSS was due to that fact that any valid CSS was valid SCSS, so you could baby step into SCSS on an existing codebase fairly easily.\u2026","rel":"","context":"In "Link"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"featured_media_src_url":"https:\/\/i0.wp.com\/css-tricks.com\/wp-content\/uploads\/2020\/08\/typscript-hollow.png?fit=1024%2C512&ssl=1","_links":{"self":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/317887"}],"collection":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/users\/38145"}],"replies":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/comments?post=317887"}],"version-history":[{"count":10,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/317887\/revisions"}],"predecessor-version":[{"id":318881,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/posts\/317887\/revisions\/318881"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media\/318809"}],"wp:attachment":[{"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/media?parent=317887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/categories?post=317887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/css-tricks.com\/wp-json\/wp\/v2\/tags?post=317887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}