[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;// == ...
随机推荐
- mysql-5.7.25安装及常用语法
我下的是免安装版的压缩文件包,可以选择下载.msi的程序包,那样就可以通过常见的图形界面来进行安装配置了 参考链接:https://blog.csdn.net/qq_23994787/article/ ...
- 【BZOJ 1045】 [HAOI2008] 糖果传递
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 思路来自hzwer.. 设xi表示第i个人往左传递了xi个糖果. (如果小于0表示旁边的人给他了糖果. 则ans=∑|xi| 最后所 ...
- 给一个执行在windows 7和NAT下的VMWARE虚拟机分配固定IP
虚拟机上装了个oracleserver,每次vmware重新启动或者resume时总要分配新的IP地址,这样就得改动windows下的client配置,所以须要想办法把IP地址固定住. DHCP服务在 ...
- php实现简单算法1
php实现简单算法1 <? //-------------------- // 基本数据结构算法 //-------------------- //二分查找(数组里查找某个元素) functio ...
- win7防火墙里开启端口的图文教程
转载于:http://www.cnblogs.com/vipsoft/archive/2012/05/02/2478847.html 开启端口:打开“控制面板”中的“Windows防火墙”,点击左侧的 ...
- java读取jpg图片旋转按比例缩放
//入口 public static BufferedImage constructHeatWheelView(int pageWidth, int pageHeight, DoubleHolder ...
- Linux下查看进程IO工具iopp
Linux下的IO检测工具最常用的是iostat,不过iostat只能查看到总的IO情况.如果要细看具体那一个程序点用的IO较高,可以使用iotop .不过iotop对内核版本和Python版本有要求 ...
- Flex 编译器及编译步骤
通过为Flex项目添加编译器参数:-keep-generated-actionscript=true 可以看到经过编译器处理过的源程序文件集合. 在这些文件的文件名中包含有“xxx-generated ...
- 重写prototype原型后哪些东西改变了
参考<JavaScript高级教程>实例看: 1.重写原型对象后,首先原型对象的constructor属性值(constructor的指向)会发生改变. function Person() ...
- Zabbix监控平台部署
系统环境 Server端:192.168.149.128 Agent端:192.168.149.129 一.lamp环境安装 1.yum安装lamp yum install -y http http- ...