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的更多相关文章

  1. TypeScript 3.7 RC & Assertion Functions

    TypeScript 3.7 RC & Assertion Functions assertion functions, assert https://devblogs.microsoft.c ...

  2. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...

  3. 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- ...

  4. 09.AutoMapper 之自定义类型转换器(Custom Type Converters)

    https://www.jianshu.com/p/47054d92db2a 自定义类型转换器(Custom Type Converters) 有时需要完全控制一种类型到另一种类型的转换.这一般发生在 ...

  5. #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

    #include <iostream> #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) usi ...

  6. [TypeScript] Define a function type

    type DigitValidator = (char) => boolean; -]{}/.test(char); export const digitValidators: {[key: s ...

  7. [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 ...

  8. [TypeScript] Avoid any type

    To avoid using "any" type is a best pratice. The reason for that is it disable the power o ...

  9. [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 ...

随机推荐

  1. 使用Intellij IDEA的Bookmarks

    用idea的时候,无意中发现了了一个小功能,叫做BookMark Ctrl+F11按出来的然后去查阅了一下文档,主要功能也就是可以清晰的看到自己标的书签附近的代码,比如我们在第11行按一下F11插入一 ...

  2. struts2 使用json

    前台代码: Struts.xml: UserAction: 注意: 1)struts类库里面没有提供ezmorph-1.0.6.jar文件,所以要手动添加:

  3. Spring Boot 打包分离依赖 JAR 和配置文件

    <properties> <java.version>1.8</java.version> <project.build.sourceEncoding> ...

  4. Lua表(table)的用法_个人总结

    Lua表(table)的用法_个人总结 1.表的创建及表的介绍 --table 是lua的一种数据结构用来帮助我们创建不同的数据类型.如:数组和字典--lua table 使用关联型数组,你可以用任意 ...

  5. vue 画二维码

    首先安装一下相关的插件 qrcode2 npm install --save qrcode2 然后在需要画二维码的页面引入一下 import QRCode from 'qrcode2' 最后在meth ...

  6. struts2与常用表格ajax操作的json传值问题

    struts与常用的dataTables和jqueryGrid等表格进行ajax传值时,经常会传值不适配的问题,这是因为struts在进行ajax操作时已经对你要操作的json数据进行了处理,所以不需 ...

  7. PHP典型功能与Laravel5框架开发学习笔记

    步骤一:PHP的Redis应用及HTTP协议 一.Redis初识 1.Linux下安装redis:具体看官网:https://redis.io/download:以下为以个人习惯的安装目录进行的red ...

  8. 【Jsp,Servlet初学总结】 含 cookie和session的使用

    一.Jsp 1. 指令: <%@ page language="java" import="java.*" contextType="text/ ...

  9. windows枚举串口

    1. 枚举键值 HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM 2. SETUPAPI方式 int EnumPortsWdm() { int i, d ...

  10. HTML基础知识 table中 th,td,tr

    https://www.2cto.com/kf/201711/701872.html table是一个布局神器,之前看过很多代码,都是用table布局的.但是,我在学习的过程中,发现table有很迷的 ...