[TypeScript] Use TypeScript’s never Type for Exhaustiveness Checking
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的更多相关文章
- Learining TypeScript (一) TypeScript 简介
Learining TypeScript (一) TypeScript 简介 一.TypeScript出现的背景 2 二.TypeScript的架构 2 1. 设计目标 2 2 ...
- TypeScript:TypeScript 百科
ylbtech-TypeScript:TypeScript 百科 TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类 ...
- [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 ...
- [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 ...
- [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、什么是TypeScript、TypeScript的安装、转换为.js文件
1.什么是TypeScript (本人用自己的理解梳理了一下,不代表官方意见) TypeScript:Type+ECMAScript6 TypeScript是一种预处理编程语言,遵循es6标准规范,在 ...
- 【TypeScript】TypeScript 学习 4——模块
前端数据验证在改善用户体验上有很大作用,在学了之前的知识的时候,我们很可能会写出以下代码: interface StringValidator { isAcceptable(s: string): b ...
- 【TypeScript】TypeScript 学习 2——接口
在 TypeScript 中,接口是用作约束作用的,在编译成 JavaScript 的时候,所有的接口都会被擦除掉,因为 JavaScript 中并没有接口这一概念. 先看看一个简单的例子: func ...
- [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 ...
随机推荐
- vue 写一个聊天工具
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- faster rcnn一些博客
这个是对faster 问题的一个总结 http://blog.csdn.net/u010402786/article/details/72675831?locationNum=11&fps=1 ...
- java解决动态的锁顺序死锁的方案
直接上代码 public class Test3 { public static Object fromAccount = new String("1"); public stat ...
- jdk5.0新增两个线程创建方法
1.实现callable接口 import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; ...
- 基于oauth2.0实现应用的第三方登录
OAuth2 OAuth2所涉及到的对象主要有以下四个: Client 第三方应用,我们的应用就是一个Client Resource Owner 资源所有者,即用户 Authorization Ser ...
- IDEA基本使用及配置(2)
IDEA配置:File >> Setiings进入配置界面 1.主题配置:默认两种主题,黑色.白色,可以自己在网上下载,然后File >> Import Setiings导入, ...
- python3.x Day5 面向对象
类:类是指:对具有相同属性的事物的抽象.蓝图.原型.在类中定义了这些事物都具备的属性和共同的方法. 对象:一个对象就是一个类实例化以后的实例,一个类必须经过实例化后才能在程序中被使用,一个类可以实例化 ...
- css布局的各种FC简单介绍:BFC,IFC,GFC,FFC
什么是FC? Formatting Context,格式化上下文,指页面中一个渲染区域,拥有一套渲染规则,它决定了其子元素如何定位,以及与其他元素的相互关系和作用. BFC 什么是BFC Block ...
- Qt笔记——各种组件和其他细碎用法
LineEdit 获取文本:ui->usrLineEdit->text() 清空内容:ui->pwdLineEdit->clear(); 定位光标:ui->usrLine ...
- Qt——信号与槽用法总结(未完待续)
1.设计模式中信号与槽编辑选项卡 2.右键组件,转到槽,写函数 void LoginDialog::on_loginBtn_clicked() { accept(); } 3.信号与槽编辑模式 按下F ...