[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 ...
随机推荐
- (转)Nutz | Nutz项目整合Spring实战
http://blog.csdn.net/evan_leung/article/details/54767143 Nutz项目整合Spring实战 前言 Github地址 背景 实现步骤 加入spri ...
- group by 和 聚合函数的使用
有这样一个表数据: 学生姓名,学生手机号,上课日期,上课科目 科目分: 语文.数学.英语.计算机 要求统计一个这样子的结果: 学生姓名,学生手机号,第一次上课日期,迄今一共上了多少节课,上的最多的科目 ...
- bdflush - 将dirty缓存写回到磁盘的核心守护进程
总览(SYNOPSIS) bdflush [opt] 描述(DESCRIPTION) bdflush 被用来启动核心守护进程将内存中的dirty缓存写到磁盘上.真正清洁工作是一个核心程序完成的. bd ...
- CAD参数绘制固定批注(网页版)
js中实现代码说明: 自定义实体绘制函数 function ExplodeFun(pCustomEntity, pWorldDraw, txt) { var sGuid = pCustomEntity ...
- April Fools Day Contest 2019: editorial回顾补题
A. Thanos Sort time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- D2. Toy Train
D2. Toy Train time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 09Windows编程
Windows编程 2.1 窗口 Windows应用程序一般都有一个窗口,窗口是运行程序与外界交换信息的界面.一个典型的窗口包括标题栏.最小化按钮.最大/还原按钮.关闭按钮.系统菜单图标.菜 ...
- [Luogu] P1156 垃圾陷阱
题目描述 卡门――农夫约翰极其珍视的一条Holsteins奶牛――已经落了到“垃圾井”中.“垃圾井”是农夫们扔垃圾的地方,它的深度为D(2≤D≤100)英尺. 卡门想把垃圾堆起来,等到堆得与井同样高时 ...
- php实现短信验证
PHP实现短信验证的整体思路: 一.申请短信api ->申请网址https://s1.chanyoo.cn/login?url=%2f 二.编写核心代码(thinkPHP5) 示例: <? ...
- jQuery对table排序
<script> //col对应列,cmp两数比较方法,返回值为TRUE,FALSE function sort(col, cmp) { var table = $("#test ...