[TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript
This lesson introduces the --strictNullChecks compiler option and explains how non-nullable types differ from nullable types. It also illustrates how you can write safer code by being explicit about null and undefined in the type system.
First of all:
Number, string, boolean types can be assign to null or undefiend.

If we don't want null or undefined, we can turn on "strictNullCheckes" in the tsconfig.json.
{
"compilerOptions": {
"strictNullChecks": true
}
}

We can fix the compiler error by using unit type:
let text: string | null | undefined;
text = "text";
text = null;
text = undefined;
Another thing about vscode autocompletion, as we can see that document.getElementById() return HTMLElement or null:

Because of null type, so we cannot get autocompletion in IDE:

Typescript force us to do type checking, we can do:

As we can see that, once we wrap it into if statement, then we can get autocompletion because typescript now knows that 'app' is only HTMLElmenet type.
Last, in Typescript, we can write sytax like:
const app = document.getElementById("appId")!;
We add '!' in the end, it tell Typescript, null type is not allowed.
Then we can get autocompletion with if statement. But this is only a valid snytax in typescript.

[TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript的更多相关文章
- [Typescript 2] Nullable Types - Avoiding null and undefined Bugs
For example you have a TS app: enum PaylerPosition { Guard, Forward, Center } interface Player { nam ...
- 关于TypeScript中null,undefined的使用
TypeScript本质是javascript,因此基本上js所有的功能在ts上完全可以照搬照抄过来使用.根据ts的文档,有些我觉得值得商榷的——比如null,undefined就是例子. 文档上说一 ...
- 判断一个值是否为null或者undefined
var a=null; var b=undefined; if(a===null){ //a==null alert("a=null") }else{ alert("a= ...
- null和undefined的一些区别
读了阮一峰的博客,自己总结一下,便记录一篇博客 在javacript的基本类型中,有2种基本类型,只有1个值,便是null和undefined,都表示的是"无".在一定程度上是相等 ...
- JS中判断null、undefined与NaN的方法
写了个 str ="s"++; 然后出现Nan,找了一会. 收集资料如下判断: 1.判断undefined: 代码如下: <span style="font-siz ...
- V8 的 typeof null 返回 "undefined" 的 bug 是怎么回事
1997 年,IE 4.0 发布,带来的众多新特性中有一个对未来“影响深远”的 DOM API:document.all.在随后的 6 年里,IE 的市场占有率越来越高,直到 2003 年的 95%. ...
- 【阿里李战】解剖JavaScript中的 null 和 undefined
在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理.于是,细想之后,写下本文,请各位 ...
- null和undefined区别
undefined表示不存在的状态.没有定义的变量,没有定义的对象属性,没有return的函数的返回值等等都是undefined. null表示没有对象.使用上没有差别,只是根据大众的使用习惯,场合不 ...
- null、undefined、false、0相等性比较
之前在看<JavaScript权威指南>的时候看到三个相等性比较的式子: null == undefined ;// ==>true undefined == false;// == ...
随机推荐
- [笔记-统计学习方法]感知机模型(perceptron) 原理与实现
前几天认把感知机这一章读完了,顺带做了点笔记 现在把笔记做第三次的整理 (不得不说博客园的LaTex公式和markdown排版真的不太舒服,该考虑在服务器上建一个博客了) 零.总结 适用于具有线性可分 ...
- 转载-- Qt Creator编译时make: arm-linux-g++: command not found 错误!
前提是已经配置好交叉编译器,但是qt creator找不到. 解决方法: 修改 /usr/local/Trolltech/QtEmbedded-4.7.0-arm/mkspecs/qws/linux- ...
- hdu(1069)——Monkey and Banana(LIS变形)
题意: 如今给你n个石块,然后它由坐标来表示(x,y,z).可是它能够有不同的方法,也就是说它的三个坐标能够轮换着来的. 石块的数量不限,可是每次都必须保持上底面的长和宽严格递减,然后问你用这些石块所 ...
- Android页面事件挂接模拟
Java没有C#的引用类型.因此事件的挂接一般都是利用接口来实现,有两种方式: 1)定义一个实现事件接口的类,然后实现接口方法,然后通过将这个类的实例加入到事件监听器里面: public class ...
- hdoj--5053--the Sum of Cube(水)
the Sum of Cube Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tot ...
- Linux下清除系统日志方法
摘要:相信大家都是用过Windows的人.对于Windows下饱受诟病的各种垃圾文件都需要自己想办法删除,不然你的系统将会变得越来越大,越来越迟钝!windows怎么清理垃圾相信大家都知道的,那么li ...
- Red Hat Linux 安装 (本地、网络安装)
Red Hat Linux 安装 (本地.网络安装) 650) this.width=650;" onclick='window.open("http://blog.51cto.c ...
- 发现javax.xml.parsers.SAXParser有bug
javax.xml.parsers.SAXParser有bug, 我发现的地方在characters(char[] ch, int start, int length) length偶尔会变小,导致截 ...
- 在Windows下如何创建虚拟环境(默认情况下)
很多小伙伴平时在使用Python的时候,有的项目需要使用Python2来进行开发,有的项目则是需要Python3来进行开发.当不清楚怎么分开环境的时候,此时两个环境开始打架,彼此傻傻分不清楚.虚拟环境 ...
- javaScript [[scope]]学习笔记
在javaScript 学习中,闭包肯定是一个让人头疼的问题,而闭包必定离不开[[scope]]作用域 scope存储了执行期上下文的集合 而了解scope以及scope链又是一个十分抽象的问题 我们 ...