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的更多相关文章

  1. 玩转TypeScript(引言&文章目录) --初看TypeScript.

    JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...

  2. Adding DOM elements to document

    1.JavaScript 添加DOM Element 执行效率比较: 抄自:http://wildbit.com/blog/2006/11/21/javascript-optimization-add ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. ReactDOM & DOM Elements

    一.ReactDOM 1.1 render() ReactDOM.render(element,container,[callback]) 在container中渲染一个React元素,然后返回组件一 ...

  8. electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint

    我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...

  9. TypeScript完全解读(26课时)_11.TypeScript完全解读-类型推论和兼容性

    11.TypeScript完全解读-类型推论和兼容性 在一些时候省略指令,ts会帮我们推断出省略的类型的地方适合的类型,通过学习ts的类型推论了解ts的推论规则 类型兼容性就是为了适应js灵活的特点, ...

随机推荐

  1. 创建一个支持ES6的Nodejs项目

    文章来自于:https://www.codementor.io/iykyvic/writing-your-nodejs-apps-using-es6-6dh0edw2o 第一步:创建项目文件夹并初始化 ...

  2. CodeForces 734E Anton and Tree

    $dfs$缩点,树形$dp$. 首先将连通块缩点,缩点后形成一个黑白节点相间的树.接下来的任务就是寻找一个$root$,使这棵树以$root$为根,树的高度是最小的(也就是一层一层染色).树形$dp$ ...

  3. Office 2016系列下载地址

    版本:Office 2016 Pro Plus 64位文件名:SW_DVD5_Office_Professional_Plus_2016_64Bit_ChnSimp_MLF_X20-42426.ISO ...

  4. Visual Studio Xamarin提示Bonjour backend初始化失败

    Visual Studio Xamarin提示Bonjour backend初始化失败 错误信息:The Bonjour backend failed to initialize, automatic ...

  5. Struts2中的设计模式

    http://blog.csdn.net/significantfrank/article/details/7712053 1. Command Pattern 基本定义: 把Command(Requ ...

  6. 【转】python assert用法

    1.assert语句用来声明某个条件是真的.2.如果你非常确信某个你使用的列表中至少有一个元素,而你想要检验这一点,并且在它非真的时候引发一个错误,那么assert语句是应用在这种情形下的理想语句.3 ...

  7. Problem D: 指针:调用自定义排序函数sort,对输入的n个数进行从小到大输出。

    #include<stdio.h> int sort(int *p,int n) { int i,j,temp; ;i<n-;i++) for(j=i;j<n;j++) if( ...

  8. Problem E: 调用函数,整数逆序输出

    #include<stdio.h> int reverse(int number)//定义函数 { ;//result用于储存结果 ) { result=result*; i=number ...

  9. 操作系统--IO系统任务简述

    内核IO---操作系统对于IO的职责 1.对文件和设备命名空间的管理 2.文件和设备访问的控制 3.IO操作控制 4.文件系统的空间分配 5.设备分配 6.IO缓冲管理 7.IO调度方式 8.设备状态 ...

  10. 5 Best Gantt Chart JIRA Plugins

    Andrew Stepanov/June 23, 2017/6 minutes Software developers enjoy using JIRA software for their proj ...