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. Google浏览器开发者工具:CSSViewer(一个Css查看器)

    CSSViewer的简介 CSSViewer是一款可以帮助用户快速查看当前的网页元素的CSS属性的谷歌浏览器插件,在Chrome中安装了CSSViewer插件以后,用户就可以在设计网页的时候,快速地模 ...

  2. 理解 call, apply 的用法

    callcall() 方法使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数. function list() { return Array.prototype.slice.call ...

  3. C#中练级orcle数据查询

    直接贴代码哈哈哈, public DataTable getInfo(int flag) { OracleConnection conn = null; DataSet ds = new DataSe ...

  4. CentOS中一些基本的操作记录

    1)切换到root su root 输入你的密码.我的是123

  5. Spring Data Redis整体介绍 (一)

    为什么使用Spring Data Redis 首先Spring Data Redis 是Spring 框架提供的用于操作Redis的客户端. Spring框架是一个全栈Java程序框架,通过DI.AO ...

  6. oracle分析函数系列之sum(col1) over(partition by col2 order by col3):实现分组汇总或递增汇总

    语法:sum(col1) over(partition by col2 order by col3 )  准备数据: DEPT_ID    ENAME          SAL1 1000       ...

  7. 导出网页表格数据为Excel文件的前端解决方案

    在工作中,我们有时会遇到这样的需求,比如:要把页面的表格数据导出为Excel文件.在此记录下自己用的解决方法.代码如下: function tableToExcel(data){ //要导出的数据,t ...

  8. Linux从入门到适应(三):Ubuntu16.04将python从3.5升级到3.6

    1 将python从默认的python2.7更换为python3.5 : sudo update-alternatives --install /usr/bin/python python /usr/ ...

  9. Hihocoder #1938 最大权闭合子图模板

    这里的讲解很不错,适合作为入坑题: Hihocoder#1938 代码: #include<algorithm> #include<iostream> #include< ...

  10. [Usaco2009 Nov]lights(高斯消元)

    luogu 点灯游戏应该很多人都在小时候頽过吧 反正我直到现在也不会 很明显一个灯最多只需要点一次 然后高斯消元 解完肯定剩自由元(就是那些全是0的行) 然后这些都爆搜 由于剩下的自由元不会太多 所以 ...