TypeScript 2.0 introduced a new primitive type called never, the type of values that never occur. It helps model the completion behavior of functions more accurately and can also be used for exhaustiveness checking.

never type means that 'That part of code cannot be reached'.

In somecases, never type can be useful as well.

const enum ShirtSize {
XS,
S,
M,
L,
XL
} function assertNever(value: never): never {
// throw Error(`Unexpected value '${value}'`)
// Adjusted for plunker output
console.log(Error(`Unexpected value '${value}'`));
} function prettyPrint(size: ShirtSize) {
switch (size) {
case ShirtSize.S: console.log("small");
case ShirtSize.M: return "medium";
case ShirtSize.L: return "large";
case ShirtSize.XL: return "extra large";
// [ts] Argument of type 'ShirtSize.XS' is
// not assignable to parameter of type 'never'.
default: return assertNever(size);
}
} prettyPrint(ShirtSize.S)
prettyPrint(ShirtSize.XXL)

In the example, we want to make sure that every time in the enum ShirtSzie has been handled by the 'prettyPrint' function.

But sometime, we might miss one case. That's where 'assertNever' function can help to make sure, we have gone though all the cases.

[TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking的更多相关文章

  1. Learining TypeScript (一) TypeScript 简介

    Learining TypeScript (一) TypeScript 简介 一.TypeScript出现的背景    2 二.TypeScript的架构    2 1.    设计目标    2 2 ...

  2. TypeScript:TypeScript 百科

    ylbtech-TypeScript:TypeScript 百科 TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类 ...

  3. [TypeScript] Increase TypeScript's type safety with noImplicitAny

    TypeScript tries to infer as much about your code as it can. But sometimes there really is not enoug ...

  4. [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers

    Using the optional “+” sign together with mapped type modifiers, we can create more explicit and rea ...

  5. [Typescript] What is a Function Type ? Function Types and Interfaces - Are They Related ?

    Function Type: type messageFn = (name: string) => string; function sayHello(name: string): string ...

  6. 6、什么是TypeScript、TypeScript的安装、转换为.js文件

    1.什么是TypeScript (本人用自己的理解梳理了一下,不代表官方意见) TypeScript:Type+ECMAScript6 TypeScript是一种预处理编程语言,遵循es6标准规范,在 ...

  7. 【TypeScript】TypeScript 学习 4——模块

    前端数据验证在改善用户体验上有很大作用,在学了之前的知识的时候,我们很可能会写出以下代码: interface StringValidator { isAcceptable(s: string): b ...

  8. 【TypeScript】TypeScript 学习 2——接口

    在 TypeScript 中,接口是用作约束作用的,在编译成 JavaScript 的时候,所有的接口都会被擦除掉,因为 JavaScript 中并没有接口这一概念. 先看看一个简单的例子: func ...

  9. [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)

    This lesson shows you how to install TypeScript and run the TypeScript compiler against a .ts file f ...

随机推荐

  1. 洛谷 P2341 [HAOI2006]受欢迎的牛

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  2. CentOS 7 安装Oracle VirtualBox

    1. 下载VirtualBox的repo文件: 登陆 https://www.virtualbox.org/wiki/Linux_Downloads 在网页的最下端的repo链接上右键下载,或者wge ...

  3. 硕毕论文_基于 3D 卷积神经网络的行为识别算法研究

    论文标题:基于 3D 卷积神经网络的行为识别算法研究 来源/作者机构情况: 中  国  地  质  大  学(北京),计算机学院,图像处理方向 解决问题/主要思想贡献: 1. 使用张量CP分解的原理, ...

  4. myeclipse出现Failed to load JavaHL Library.

    eclipse启动出现如图的状况: 解决方法: Window-Preferences-Team-SVN,在SVN接口的下拉框可以看到,默认选择的是JavaHL(JNI) Not Available,手 ...

  5. python 深复制和浅复制

    https://www.python-course.eu/python3_deep_copy.php-------------------------------------------------- ...

  6. 浏览器通知Web Notifications实例页面

    HTML代码: <button id="button">有人想加你为好友</button> <p id="text">< ...

  7. 【转】vfork 和 fork的区别

    fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1.  fork  ():子进程拷贝父进程的数据段,代码段     vfork ( ):子进程与父进程共享数据段 ...

  8. PHPExcel读取表格内容

    PHPExcel读取表格 先引入类IOFactory.php require_once '../PHPExcel/IOFactory.php'; $filePath = "test.xlsx ...

  9. 基于Vue的简单日历组件

    日历组件 由于移动端项目中需要用到日历组件,网上找了下,没看到几个合适的,就尝试着自己写一个.然后发现也不是很复杂,目前只做了最基本的功能,大家也可以拿去做做二次开发. 如何写一个日历组件 基础效果如 ...

  10. 一个IT工薪族的4年奋斗成果

     关于标题:为了方便传播,使用了"最简化"的一段. 过段时间,考虑改为"大学毕业4年-回顾和总结(11):一个IT工薪族的4年奋斗成果(2012年6月17日~2016年6 ...