[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 ...
随机推荐
- parsley.js正确使用姿势
1.第一式 当然要先引用:parsley.js 2.第二式 页面中定义需要使用自定义校验,注意红色的地方,必须要使用小写,重要的问题说三遍,小写,小写 <form class="for ...
- tomcat添加访问的ip限制
在如下位置添加如下代码: 代码: <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow=&q ...
- ANALYZE - 收集与数据库有关的统计
SYNOPSIS ANALYZE [ VERBOSE ] [ table [ (column [, ...] ) ] ] DESCRIPTION 描述 ANALYZE 收集有关 PostgreSQL ...
- python练习2 购物车程序
# -*- coding: utf-8 -*-# @Time : 2018/10/18 16:06# @Author : Two Brother# @EMAIL : yeluyide@163.com# ...
- CAD参数绘制点(com接口)
点在CAD中的作用除了可以分割对象外,还能测量对象,点不仅表示一个小的实体,而且通过点作为绘图的参考标记. pdmode是一个控制point的形式的系统变量,当pdmode=0时是可见的一个点,当pd ...
- du查看文件大小
du+文件名就可以查看文件大小 du+ -h + 文件名也是查看文件大小,只是-h会将文件大小转换成M,G等格式
- 无插件纯Web 3D机房,HTML5+WebGL倾力打造
前言 最近项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,以前一直搞java,对js的一些语言特性和概念一时还转换不过来. 上一篇大数 ...
- extjs动态添加列
可以根据日期,动态的插入一列 controller层: StdDayWordQuery:function(btn,event){ var form=Ext.getCmp('queryFormSDW') ...
- Linux环境下验证码不显示F12报500
前言: 项目之前部署在linux系统上进行测试,今天重新部署的时候,重启了tomcat然后部署新的版本项目,结果登录页面验证码不显示,在浏览器F12页面显示的是500错误.网上查了很多方法,都没效果 ...
- vue-cli webpack 快速搭建项目
一.安装vue npm install vue -g 二.用vue-cli快速搭建项目 //全局安装vue-cli npm install install -g vue-cli //创建一个基于web ...