[TypeScript] Define Custom Type Guard Functions in TypeScript
One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a variable within a type guard.
This lesson explores how you can define functions and type predicates to create your own type guards similar to the Array.isArray()
method.
const numbers = [0, 1, 2, [3, 4], 5, [6], [7], 8, [9]]; function isFlat<T>(array: (T | T[])[]): array is T[] {
console.log(!array.some(Array.isArray));
return !array.some(Array.isArray);
} if (isFlat(numbers)) {
numbers;
}
isFlat function return value is a boolean value. We add 'array is T[]' that adds additional information for types.
isFlat(numbers): numbers type is '(number|number())[]'
but inside if statement: numbers is 'number[]', because we tell typescript, array is T[] in the return value.
[TypeScript] Define Custom Type Guard Functions in TypeScript的更多相关文章
- TypeScript 3.7 RC & Assertion Functions
TypeScript 3.7 RC & Assertion Functions assertion functions, assert https://devblogs.microsoft.c ...
- #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...
- Binding a Xamarin.Forms WebView to ReactiveUI View Model using Custom Type Converters
引用:https://jamilgeor.com/binding-a-xamarin-forms-webview-to-reactiveui-view-model-using-custom-type- ...
- 09.AutoMapper 之自定义类型转换器(Custom Type Converters)
https://www.jianshu.com/p/47054d92db2a 自定义类型转换器(Custom Type Converters) 有时需要完全控制一种类型到另一种类型的转换.这一般发生在 ...
- #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#include <iostream> #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) usi ...
- [TypeScript] Define a function type
type DigitValidator = (char) => boolean; -]{}/.test(char); export const digitValidators: {[key: s ...
- [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 ...
- [TypeScript] Avoid any type
To avoid using "any" type is a best pratice. The reason for that is it disable the power o ...
- [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type
ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...
随机推荐
- jq 中.html(),.text()和.val()的总结
html与.text的方法操作是一样,只是在具体针对处理对象不同 html处理的是元素内容,.text处理的是文本内容 html只能使用在HTML文档中,.text 在XML 和 HTML 文档中都能 ...
- txt通过 vb编程导入 excel
Private Sub CommandButton1_Click() '按钮触发, '按钮触发,目前支持ASNI文本的数据, 单行 fileToOpen = Application.GetOpenFi ...
- iTOP-4418开发板最新资料及例程
开发板资料: 开发板硬件资源: 1.POWER电源接口 电源输入为 5V/2A+,给核心板 AXP228 电源管理芯片提供 5V 电源. 2.SWITCH电源开关 轻触电源开关可以控制开发板电源通断. ...
- Java处理ZIP文件的解决方案——Zip4J(不解压直接通过InputStream形式读取其中的文件,解决中文乱码)
一.JDK内置操作Zip文件其实,在JDK中已经存在操作ZIP的工具类:ZipInputStream. 基本使用: public static Map<String, String> re ...
- ssd训练自己的数据集
1.在ssd/caffe/data下创建VOC2007的目录,将ssd/caffe/data/VOC0712里的create_data.sh.create_list.sh和labelmap_voc.p ...
- 无插件纯Web 3D机房,HTML5+WebGL倾力打造
前言 最近项目开发任务告一段落,刚好有时间整理这大半年的一些成果.使用html5时间还不久,对js的认识还不够深入.没办法,以前一直搞java,对js的一些语言特性和概念一时还转换不过来. 上一篇大数 ...
- rownum导致sql不能进行谓词推入
背景 案件(13405_RI-再保结算查询导出太慢),造成性能慢的原因是执行以下sql时,每次执行平均需要消耗2秒, 画面上,点击一次导出按钮,就会发起数以百记的调用. SQL执行代码段 select ...
- CURL PHP模拟浏览器get和post
模拟浏览器get和post数据需要经常用到的类, 在这里收藏了几个不错的方法 方法一 <?php define ( 'IS_PROXY', true ); //是否启用代理 /* cookie文 ...
- 集训第四周(高效算法设计)B题 (二分查找优化题)
---恢复内容开始--- Description Before the invention of book-printing, it was very hard to make a copy of ...
- 如何转成libsvm支持的数据格式并做回归分析
本次实验的数据是来自老师给的2006-2008年的日期,24小时的温度.电力负荷数据,以及2009年的日期,24小时的温度数据,目的是预测2009年每天24小时的电力负荷,实验数据本文不予给出. 用l ...