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

  1. [Typescript 2] Nullable Types - Avoiding null and undefined Bugs

    For example you have a TS app: enum PaylerPosition { Guard, Forward, Center } interface Player { nam ...

  2. 关于TypeScript中null,undefined的使用

    TypeScript本质是javascript,因此基本上js所有的功能在ts上完全可以照搬照抄过来使用.根据ts的文档,有些我觉得值得商榷的——比如null,undefined就是例子. 文档上说一 ...

  3. 判断一个值是否为null或者undefined

    var a=null; var b=undefined; if(a===null){ //a==null alert("a=null") }else{ alert("a= ...

  4. null和undefined的一些区别

    读了阮一峰的博客,自己总结一下,便记录一篇博客 在javacript的基本类型中,有2种基本类型,只有1个值,便是null和undefined,都表示的是"无".在一定程度上是相等 ...

  5. JS中判断null、undefined与NaN的方法

    写了个 str ="s"++; 然后出现Nan,找了一会. 收集资料如下判断: 1.判断undefined: 代码如下: <span style="font-siz ...

  6. V8 的 typeof null 返回 "undefined" 的 bug 是怎么回事

    1997 年,IE 4.0 发布,带来的众多新特性中有一个对未来“影响深远”的 DOM API:document.all.在随后的 6 年里,IE 的市场占有率越来越高,直到 2003 年的 95%. ...

  7. 【阿里李战】解剖JavaScript中的 null 和 undefined

    在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理.于是,细想之后,写下本文,请各位 ...

  8. null和undefined区别

    undefined表示不存在的状态.没有定义的变量,没有定义的对象属性,没有return的函数的返回值等等都是undefined. null表示没有对象.使用上没有差别,只是根据大众的使用习惯,场合不 ...

  9. null、undefined、false、0相等性比较

    之前在看<JavaScript权威指南>的时候看到三个相等性比较的式子: null == undefined ;// ==>true undefined == false;// == ...

随机推荐

  1. 今日SGU 5.28

    SGU 121 题意:给你一张图,问你每个顶点必须有黑白两条边(如果它的边数>=2),问你怎么染色,不行就输出no 收获:你会发现不行的情况只有一个单纯的奇数环的时候,反之我们交替染色即可 #i ...

  2. Struts(21)OGNL具体解释

    Struts2 中内置了OGNL表达式的支持,使得Struts2的具有比Struts1更为强大的数据訪问的功能.本文主要解说OGNL的用法.并不会去解说一些原理性的东西.想要了解的朋友能够自己去查阅相 ...

  3. SQL Server数据全同步及价值分析[终结版]

    SQL Server数据全同步[终结版] 版权全部.转载请注明出处.谢谢! 经过两天的同步编写和測试.出了第一个Release版本号: 1. 本函数仅支持单向同步.即从一个主数据库想多个从数据库同步 ...

  4. DataTable转成Json

    /// <summary>         /// DataTable转成Json /// </summary>         /// <param name=&quo ...

  5. assert 的理解

    assert 可以实现如下功能: 保证参数之间的大小等约束关系: 函数执行过程中得到的中间结果是否符合预期: def gen_batch(batch_size, skip_window, num_sk ...

  6. Controller接口控制器2

    5.ServletForwardingController 将接收到的请求转发到一个命名的servlet,具体示例如下: package cn.javass.chapter4.web.servlet; ...

  7. dubbo+zookeeper+springMVC +maven

    pom:                    <dependency>             <groupId>com.github.sgroschupf</grou ...

  8. Flask Flash闪现

    Flash介绍以及工作方式 flash中添加消息 取出flash中的消息 Flash介绍以及工作方式 - 介绍: flash :闪现 一个好的应用和用户界面都需要良好的反馈. 如果用户得不到足够的反馈 ...

  9. BZOJ1901 ZOJ2112 线段树+treap (线段树套线段树)

    BZOJ1901: 线段树套线段树做法: (外层线段树 里层动态开节点的权值线段树) 有一个小小的trick 可以省掉二分变成nlog^2n的 就是把查询的区间都取出来- logn个一起走- 2016 ...

  10. codeforces 589A Email Aliases(map)

    Description Polycarp has quite recently learned about email aliases. Of course, he used to suspect t ...