[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 ...
随机推荐
- 18/9/16牛客网提高组Day2
牛客网提高组Day2 T1 方差 第一眼看就知道要打暴力啊,然而并没有想到去化简式子... 可能因为昨晚没睡好,今天上午困死 导致暴力打了一个半小时,还不对... #include <algor ...
- Mybaits中session的应用一
获取一级缓存session SqlSession session = this.yangchebaoDbManagerImpl.getSqlSessionFactory().openSession(f ...
- OpenShift 自定义 OPENSHIFT_DOCUMENT_ROOT 变量,替换网站根目录路径!
OpenShift 自定义 OPENSHIFT_DOCUMENT_ROOT 变量,替换网站根目录路径! 预先定义的子目录 :) DIY: DocumentRoot=${OPENSHIFT_RE ...
- angular设置全局变量,可修改监听变量
创建service.module.ts import { NgModule, ModuleWithProviders } from '@angular/core'; import { SomeShar ...
- controller接收参数的对象是vo还是dto?
我也没有深入了解过,就我使用情况来说的话,VO和DTO在实际开发过程中其实可以是一样的.从定义上来说他们区别于使用的所在层,VO(view object)视图对象,DTO(Data Transfer ...
- spring扫描自定义注解并进行操作
转载:http://blog.csdn.net/cuixuefeng1112/article/details/45331233 /** * 扫描注解添加服务到缓存以供判断时候为对外开放service ...
- Perl——正则表达式(四) 查找替换s///
转自http://blog.csdn.net/blog_abel/article/details/40589227 侵删 一. 介绍 使用 s/regex/replacement/modifiers ...
- C++ tab键实现自动补全输入功能
一.简介 由于项目中写了个测试的控制台程序,是每次读取一行,即通过getline()来实现的,所以每次必须输入全路径名称,才能实现运行. 大家都觉得麻烦,就写了个tab键自动选择补全的. 目前基本可实 ...
- Dynamic device virtualization
A system and method for providing dynamic device virtualization is herein disclosed. According to on ...
- linux cmd cp -a
cp -a 在保留原文件属性的前提下复制文件 cp -r dirname destdir 复制目录后其文件属性会发生变化 想要使得复制之后的目录和原目录完全一样,可以使用cp -a dirn ...