[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;// == ...
随机推荐
- 陌上开花(CDQ分治)
题解 三维偏序裸题... 一般三维偏序是第一维排序,第二维CDQ分治,第三维树状数组. 模板题还是看代码吧... #include<iostream> #include<cstrin ...
- [洛谷P2394]yyy loves Chemistry I
题目大意:给你一个实数x($0<x\leq 1$),要你求x/23的值(保留8位小数). 解题思路:此题用double读的精度是不够的,用long double直接读入也会WA,正确做法是“sc ...
- [USACO14FEB]路障Roadblock
题目:洛谷P2176. 题目大意:有n个点m条无向边,一个人要从1走到n,他会走最短路.现在可以让一条边的长度翻倍,求翻倍后这个人要多走多少距离. 解题思路:首先可以知道,翻倍肯定是在最短路上的某条边 ...
- while循环合理运用-判断成绩脚本
在平时的工作生活中,难免不了去写一些交互性质的脚本,然而呢往往有些用户偏偏会输入不合规范的输入,为了避免就此退出脚本重新执行,这时候就可以用while去写一个死循环去针对用户的输出啊.哈哈~他输不对, ...
- dd---复制文件并对原文件的内容进行转换和格式化处理
dd命令用于复制文件并对原文件的内容进行转换和格式化处理.dd命令功能很强大的,对于一些比较底层的问题,使用dd命令往往可以得到出人意料的效果.用的比较多的还是用dd来备份裸设备.但是不推荐,如果需要 ...
- WHU 1537 Stones I
题目见: http://acm.whu.edu.cn/land/problem/detail?problem_id=1537 这个题相当无语,学长给的解法是:枚举取的个数k,然后对每个k贪心,取其中的 ...
- java中hashmap和hashtable和hashset的区别
hastTable和hashMap的区别:(1)Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现.(2)这个不同即是最重要的一点:Ha ...
- Linux系统编程——进程间通信:信号中断处理
什么是信号? 信号是 Linux 进程间通信的最古老的方式.信号是url=474nN303T2Oe2ehYZjkrggeXCaJPDSrmM5Unoh4TTuty4wSgS0nl4-vl43AGMFb ...
- Java并发编程 - Executor,Executors,ExecutorService, CompletionServie,Future,Callable
一.Exectuor框架简介 Java从1.5版本开始,为简化多线程并发编程,引入全新的并发编程包:java.util.concurrent及其并发编程框架(Executor框架). Executor ...
- orm 通用方法——QueryModelCount条件查询记录数
定义代码: /** * 描述:根据条件查询对象数 * 作者:Tianqi * 日期:2014-09-17 * param:model 对象实例 * param:cond 查询条件 * return:i ...