Conditional types take generics one step further and allow you to test for a specific condition, based on which the final type will be determined. We will look at how they work on a basic level and then leverage their power to allocate function types dynamically based on the type of their parameters, all without any overloads.

For example, the following code, the 'contianer' type is 'any'

interface StringContainer {
value: string;
format(): string;
split(): string[]
} interface NumberContainer {
value: number;
neatestPrime: number;
round(): number;
} type Item<T> = {
id: T,
container: StringContainer | NumberContainer
} let item: Item<string> = {
id: 'ad',
container: null
} item.container.value;
item.container.split(); // Compiler error

Conditional Type can help with this:

type Item<T> = {
id: T,
container: T extends string ? StringContainer : NumberContainer
}

We can build 'ArrayFilter' type to only get Array based type:

type ArrayFilter<T> = T extends any[] ? T : never;
type StringsOrNumbers = ArrayFilter<string | number | string[] | number[]>

It filters out 'string, number' type becasue they are not match 'any[]' Array type. And 'never' type is ignored.

Conditional type can replace function overloads:

interface Book {
id: string;
tableOfContent: string[];
} interface Tv {
id: number;
diagonal: number;
} interface IItemService {
getItem(id: string): Book;
getItem(id: number): Tv;
getItem<T>(id: T): Book | Tv;
} let itemService: IItemService;

We can replace the hightlighted function overloads with contidional types:

interface IItemService {
getItem<T>(id: T): T extends string ? Book: Tv;
}

[TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript的更多相关文章

  1. 深入浅出TypeScript(5)- 在React项目中使用TypeScript

    前言 在第二小节中,我们讨论了利用TypeScript创建Web项目的实现,在本下节,我们讨论一下如何结合React创建一个具备TypeScript类型的应用项目. 准备 Webpack配置在第二小节 ...

  2. [TypeScript] Transform Existing Types Using Mapped Types in TypeScript

    Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...

  3. [TypeScript] Query Properties with keyof and Lookup Types in TypeScript

    The keyof operator produces a union type of all known, public property names of a given type. You ca ...

  4. [TypeScript] Union Types and Type Aliases in TypeScript

    Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an ...

  5. [TypeScript] Overload a Function with TypeScript’s Overload Signatures

    Some functions may have different return types depending on the types of the arguments with which th ...

  6. [TypeScript] Dynamically initialize class properties using TypeScript decorators

    Decorators are a powerful feature of TypeScript that allow for efficient and readable abstractions w ...

  7. [TypeScript] Define a function type

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

  8. Java中的Union Types和Intersection Types

    前言 Union Type和Intersection Type都是将多个类型结合起来的一个等价的"类型",它们并非是实际存在的类型. Union Type Union type(联 ...

  9. Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

    代码如下: var query = from s in db.LoginUserServices join ss in db.Services on s.ServiceType equals ss.C ...

随机推荐

  1. 我的一次安装oracle的过程

    1.在装oracle之前,先安装.net3.5 2.然后正常安装oracle,一直next 3.装完oracle后,安装plsql dev工具,打开工具,发现没有connect as,是需要进行一些配 ...

  2. Flex slider参数详细

    $(window).load(function() { $('.flexslider').flexslider({ animation: "fade", //String: Sel ...

  3. runtimeService.startProcessInstanceById("process:6:55036", 2222, variables) SQL语句

    JAVA: variables:{ user_flow_start_dept : "3333"} runtimeService.startProcessInstanceById(& ...

  4. [libGDX游戏开发教程]使用Libgdx进行游戏开发(5)-关卡加载

    在上一章我们介绍了如何管理和利用素材,但是我们注意到,这些素材都是零散的,比如岩石的左部等,这一章,我们将利用这些零件拼合成完整的游戏对象. 回顾最开始的设计类图,注意Level类和所有Level中的 ...

  5. Nodejs微信开发使用wechat-api回复多条消息

    在上一往篇文章<Nodejs微信开发>中,微信后台能够正常的接收到客户端的消息,并能够简单的回复一条消息至客户端. 但我的目录是将微信与Bot Framework进行关联,那么肯定就有一些 ...

  6. 【转】python argparse用法总结

    转自:https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog. ...

  7. P3197越狱

    花费了好长时间,终于刷掉了这道题. 题目在这里(洛谷)  (信息学奥赛一本通) 嗯,没错,这是一道快速幂的题,不会快速幂点这里 好现在开始分析,这道题用小学奥数的思想就可以想到,直接算有多少种可能比较 ...

  8. python3 开发面试题(%s和format的区别)5.31

    在格式化字符串中有两种方法: 1.%s 2.format 大家常用的是哪一种方法?为什么要用你选的这种方法? 我们先看一个例子: 首先我们定义一个我军需要击杀的恐怖分子的地理坐标为 c=(128,12 ...

  9. Entity Framework part2

    EF原理以XML方式打开edmx文件,这个XML的文件主要包含两大部分:Runtime是类模型部分,Designer是VS中的图形界面重点讨论的是Runtime部分,又分为三大部分:SSDL数据模型部 ...

  10. linux-去重-uniq

    uniq : 默认(去重)  |  -d(显重)   |   -u(删重) 语法:uniq  [选项]  文件 选项 -c或--count 在每列旁边显示该行重复出现的次数 -d或--repeat 仅 ...