[TypeScript] Work with DOM Elements in TypeScript using Type Assertions
The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going to get, so you have to educate your codebase into what you know you're using. This is done using Type Assertion where TypeScript types know they're a certain type, but you give it additional information so you can access the properties and methods that you know will be available.
For example you want to control input autofocus by TypeScript:
const input = document.getElementById("input");
input.autofocus = true;
You will get compiler error:

You have to tell TypeScript, HTMLELement is actully a HTMLInputElement:
const input = document.getElementById("input") as HTMLInputElement;
input.autofocus = true;
The same case:
input.addEventListener("input", event => {
console.log(event.currentTarget.value)
})

To fix this:
input.addEventListener("input", event => {
const i = event.currentTarget as HTMLInputElement;
console.log(i.value)
})
[TypeScript] Work with DOM Elements in TypeScript using Type Assertions的更多相关文章
- 玩转TypeScript(引言&文章目录) --初看TypeScript.
JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...
- Adding DOM elements to document
1.JavaScript 添加DOM Element 执行效率比较: 抄自:http://wildbit.com/blog/2006/11/21/javascript-optimization-add ...
- [Cypress] Create Aliases for DOM Elements in Cypress Tests
We’ll often need to access the same DOM elements multiple times in one test. Your first instinct mig ...
- [D3] Create DOM Elements with D3 v4
Change is good, but creating from scratch is even better. This lesson shows you how to create DOM el ...
- [D3] Modify DOM Elements with D3 v4
Once you can get hold of DOM elements you’re ready to start changing them. Whether it’s changing col ...
- [D3] Select DOM Elements with D3 v4
Before you can create dazzling data driven documents, you need to know how D3 accesses the DOM. This ...
- ReactDOM & DOM Elements
一.ReactDOM 1.1 render() ReactDOM.render(element,container,[callback]) 在container中渲染一个React元素,然后返回组件一 ...
- electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...
- TypeScript完全解读(26课时)_11.TypeScript完全解读-类型推论和兼容性
11.TypeScript完全解读-类型推论和兼容性 在一些时候省略指令,ts会帮我们推断出省略的类型的地方适合的类型,通过学习ts的类型推论了解ts的推论规则 类型兼容性就是为了适应js灵活的特点, ...
随机推荐
- Docker 常用命令收集
查看Docker版本 docker version 查看 Image docker images 打包 Image docker save -o ‘packageName.tar’ ‘imageNam ...
- spring boot 使用thymeleaf模版 报错:org.thymeleaf.exceptions.TemplateInputException
错误: org.thymeleaf.exceptions.TemplateInputException: Error resolving template "Hello", tem ...
- SSH Secure File Transfer上传文件错误:encountered 1 errors during the transfer解决办法
在使用SSH 工具向Linux服务器上传文件时,弹出 encountered 1 errors during the transfer 错误. 解决方案: 1.准备上传的那个文件所在目录路径存在(), ...
- JDK Windows安装
进入至JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 点击下载后,会进入下载列表 点击下载后,就等 ...
- java 连接 kerberos 认证的 HBase 和 HDFS
这是两个功能,都很简单就写一块了.. 简单到什么程度呢,简单到只贴代码就可以了... HBase package com.miras.data; import org.apache.hadoop.co ...
- JavaScript函数的防抖和节流
防抖 触发高频事件后n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间 思路: 每次触发事件时都取消之前的延时调用方法 function debounce(fn) { let tim ...
- Xamarin XAML语言教程模板视图TemplatedView(二)
Xamarin XAML语言教程模板视图TemplatedView(二) (2)打开MainPage.xaml文件,编写代码,将构建的控件模板应用于中TemplatedView.代码如下: <? ...
- Beaglebone Black教程Beaglebone Black的引脚分配
Beaglebone Black教程Beaglebone Black的引脚分配 Beaglebone Black的引脚分配 绝大多数的微型开发平台都提供了一些称为GPIO的输入输出端口.这些端口可以让 ...
- cream 的qsqrt 及其原理
首先,是creamk 的qsort: float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5 ...
- 【图论】Self-Assembly(6-19)
[UVA1572]Self-Assembly 算法入门经典第6章6-19(P172) 题目大意:有一些正方形,每条边上都有A-~Z- A+~Z+的编号,或者00,A+的边可以拼A-,反之亦然.00的边 ...