[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 ...
随机推荐
- Dynamic type checking and runtime type information
动态类型的关键是将动态对象与实际类型信息绑定. See also: Dynamic programming language and Interpreted language Dynamic type ...
- idea 中pom.xml依赖版本号报错(报红,如下图所示)
1.maven工程中出现的错误 2.解决办法:file->setting->Maven 如果还没好的话请尝试以下方法:
- CREATE CONVERSION - 定义一个用户定义的码制转换
SYNOPSIS CREATE [DEFAULT] CONVERSION name FOR source_encoding TO dest_encoding FROM funcname DESCRIP ...
- Oracle数据库存储过程练习20181212
先创建一个测试的数据表 --测试表 CREATE TABLE TEST20181207 ( ID INTEGER PRIMARY KEY, FUND NUMBER,--上日资金 BALANCE NUM ...
- 下载kaggle数据集,验证手机号
https://blog.csdn.net/Tomxiaodai/article/details/80167765 kaggle上下载一下数据集必须手机验证,结果验证时一直提示错误输入的格式错误,试了 ...
- Linux C下变量和常量的存储的本质
源代码 #cat main.c #include <stdio.h> int i = 100; int main(void) { func(); return 0; } #cat func ...
- 离线缓存 application cache
1. 什么是离线缓存: 离线缓存可以将站点的一些文件缓存到本地,它是浏览器自己的一种机制,将需要的文件缓存下来,以便后期即使没有连接网络,被缓存的页面也可以展示. 例子:比如我们在手机或电脑上访问一个 ...
- 三、spring中高级装配(1)
大概看了一下第三章的内容,我从项目中仔细寻找,始终没有发现哪里有这种配置,但是看完觉得spring还有这么牛B的功能啊,spring的厉害之处,这种设计程序的思想,很让我感慨... 一.环境与prof ...
- 关于阻止Sublime Text更新弹窗提示
使用Sublime Text有一段时间了,但每次重新打开都会弹出这家伙↑,很烦 网上查了查一些关闭弹窗的教程,大同小异,都说是打开Preferences --> Settings, 添加一行代码 ...
- Python学习-列表的其它主要操作
列表的其它主要操作 还记得之前使用del语句去清除一个列表中的所有内容,那么会因为把列表全部清空,所以输出会报错.可以使用clear() 完成 clear(self):可以将一个列表变成空列表 lis ...