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. spring3 上配置quartz 任务调度

    maven配置: <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>qu ...

  2. discuz 插件核心函数hookscript分析.

    function hookscript($script, $hscript, $type = 'funcs', $param = array(), $func = '', $scriptextra = ...

  3. jQuery 收缩展开效果

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  4. ALTER GROUP - 修改一个用户组

    SYNOPSIS ALTER GROUP groupname ADD USER username [, ... ] ALTER GROUP groupname DROP USER username [ ...

  5. 删除目录文件夹时出现:rm: cannot remove `/data/wwwroot/backidc': Is a directory

    rm -f 删除目录文件夹时出现:rm: cannot remove `/data/wwwroot/backidc': Is a directory cannot remove is a direct ...

  6. 0.ssm web项目中的遇到的坑

    1.自定义的菜单,href为项目的相对路径,即: : 点击一个菜单,后再点击另一个菜单,然后发现浏览器地址栏的链接是在上一个链接后面拼接的,也就报错了. 解决办法: 每一个菜单的href前增加&quo ...

  7. python 深复制和浅复制

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

  8. Jedis集成到项目中

    Jedis整合到项目中,就可以在项目中使用redis了,作为Java程序狗,这个可以会,贴代码了,不截图了,哈哈 一.maven中的pom.xml中添加依赖 <dependency> &l ...

  9. Android之多种Bitmap效果(4)

    1. 将图片变为圆角 2. 获取缩略图图片 3. LOMO特效 4. 旧时光特效 5. 暖意特效 6. 根据饱和度.色相.亮度调整图片 7. 添加图片外边框 8. 添加内边框 9. 创建一个缩放的图片 ...

  10. [Python3网络爬虫开发实战] 1.9.6-Gerapy的安装

    Gerapy是一个Scrapy分布式管理模块,本节就来介绍一下它的安装方式. 1. 相关链接 GitHub:https://github.com/Gerapy 2. pip安装 这里推荐使用pip安装 ...