[TypeScript] Use the never type to avoid code with dead ends using TypeScript
Example 1: A never stop while loop return a never type.
function run(): never {
while(true){
let foo = "bar";
}
}
Example 2: Never run If block
const foo = ;
if(foo !== ) {
let bar: never = foo;
}
You can use this to do exhaustive checks in union types.
For example, let's say you have a variable returned from the server that can be a string or a number. You can easily add code that handles different cases using the JavaScript typeof operator. You can add an additional else, and assign the variable to a never to ensure that all types were eliminated.
declare var foo:
| string
| number; if(typeof foo === "string") {
/* todo */
} else if (typeof foo === "number"){
/* todo */
} else {
const check: never = foo;
}
Later, if you need to add another type to the union, for example, a Boolean, you will now get nice errors at all the places where the new type was not handled, because only a never is assignable to a never. Now, if you go ahead and add another typeof to handle this new case, the error goes away.
[TypeScript] Use the never type to avoid code with dead ends using TypeScript的更多相关文章
- TypeScript开发环境搭建(Visual studio code)
使用Visual Studio Code搭建TypeScript开发环境 1.TypeScript是干什么的 ? TypeScript是由微软Anders Hejlsberg(安德斯·海尔斯伯格,也是 ...
- [TypeScript] Infer the Return Type of a Generic Function Type Parameter
When working with conditionals types, within the “extends” expression, we can use the “infer” keywor ...
- [TypeScript] Union Types and Type Aliases in TypeScript
Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an ...
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
The "any" type can be very useful, especially when adding types to an existing JavaScript ...
- [转]VS Code 扩展 Angular 6 Snippets - TypeScript, Html, Angular Material, ngRx, RxJS & Flex Layout
本文转自:https://marketplace.visualstudio.com/items?itemName=Mikael.Angular-BeastCode VSCode Angular Typ ...
- 【区分】Typescript 中 interface 和 type
在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...
- TypeScript之定义类型 ( type )
键值对结构的对象 export type ValidationErrors = { [key: string]: any }; 联合类型(union type) export type HttpEve ...
- [TypeScript] Generic Functions, class, Type Inference and Generics
Generic Fucntion: For example we have a set of data and an function: interface HasName { name: strin ...
- [TypeScript] Define a function type
type DigitValidator = (char) => boolean; -]{}/.test(char); export const digitValidators: {[key: s ...
随机推荐
- python3对序列求绝对值
http://www.cnblogs.com/itdyb/p/5731804.html 一开始我是这样写的,据说这样写python2是可以的: myList = [-1,2,-3,4,-5,6 ...
- 下次自己主动登录(记住password)功能
1:进入cookie插件 <script src="jquery.cookie.js" type="text/javascript"></sc ...
- delete noprompt archivelog 报错ORA-00245,RMAN-08132
在RMAN执行 delete noprompt archivelog until time 'sysdate-1'; 报错 ORA-00245: control file backup fai ...
- Perl——正则表达式(四) 查找替换s///
转自http://blog.csdn.net/blog_abel/article/details/40589227 侵删 一. 介绍 使用 s/regex/replacement/modifiers ...
- 微服务实战(三):深入微服务架构的进程间通信 - DockOne.io
原文:微服务实战(三):深入微服务架构的进程间通信 - DockOne.io [编者的话]这是采用微服务架构创建自己应用系列第三篇文章.第一篇介绍了微服务架构模式,和单体式模式进行了比较,并且讨论了使 ...
- 《从零開始学Swift》学习笔记(Day 63)——Cocoa Touch设计模式及应用之单例模式
原创文章,欢迎转载.转载请注明:关东升的博客 什么是设计模式.设计模式是在特定场景下对特定问题的解决方式.这些解决方式是经过重复论证和測试总结出来的. 实际上.除了软件设计,设计模式也被广泛应用于其它 ...
- php实现运气模型(命运随机,克服困难)
php实现运气模型(命运随机,克服困难) 一.总结 1.应该用表格来布局的,这种多列的用表格布局比div和span布局方便很多 2.span标签设置宽度:变成行内快元素:display:inline- ...
- WebClient 请求返回的是Gbk编码解决方案
WebClient client = new WebClient(); client.Headers.Clear(); client.Hea ...
- netty检测系统工具PlatformDependent
1. 检测jdk版本 @SuppressWarnings("LoopStatementThatDoesntLoop") private static int javaVersion ...
- js面向对象的选项卡
前言: 选项卡在项目中经常用到,也经常写,今天在github突然看到一个面向对象的写法,值得收藏和学习. 本文内容摘自github上的 helloforrestworld/javascriptLab ...