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. vijos P1051 送给圣诞夜的极光

    调了好久... #include <cstdio> #include <queue> using namespace std; char a[110*110]; /****** ...

  2. python特有的协程

    #转载请联系 什么是协程呢? 线程包含在进程里面,协程包含在线程里面.协程也是和进程.线程一样,可以实现多任务.协程的切换开销比线程更小,不需要保存和恢复线程的状态.最通俗易懂的说法就是,协程是就是一 ...

  3. vim操作大全

    # 转自 https://blog.csdn.net/weixin_37657720/article/details/80645991 曾经使用了两年多的Vim,手册也翻过一遍.虽然现在不怎么用vim ...

  4. ZOJ-3822

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

  5. PHP无限极分类详谈

    当你学习php无限极分类的时候,大家都觉得一个字“难”我也觉得很难,所以,现在都还在看,因为工作要用到,所以,就必须得研究研究. 到网上一搜php无限极分类,很多,但好多都是一个,并且,写的很乱,代码 ...

  6. MySQL数据库增删改字段(属性)

    MySQL数据库的各种操作今天在这里总结一下: 一.增加 1.在已有的表中添加新的字段: 首先是增加表的字段,比如一张表原本没有字段“ Time ”,现在我们要增加这样一个字段,可以用下面的SQL语句 ...

  7. Cocos2d-Lua 做一个活动转盘

    这类活动你肯定见过 关于转盘类型的活动我相信大家多多少少都接触到了,很多的抽奖界面都是这类型的,今天这篇小文章就简单的总结一下我们游戏中需要实现这样一个效果的时候我们该怎样去做,其实只要是Cocos类 ...

  8. ASP.NET Core 2.2 基础知识(一) 依赖注入

    依赖: 类A用到了类B,我们就说类A依赖类B.如果一个类没有任何地方使用到,那这个类基本上可以删掉了. public class Test { private MyDependency md = ne ...

  9. decode and CASE

    CASE

  10. 【DFS】URAL - 2104 - Game with a Strip

    大概就是dfs?当前区间(l,r)的答案可以由(l,m)和(m+1,r)区间推出,如果某个区间已经完全被某种颜色覆盖,那么就返回该颜色.否则按照递归层数判定,奇数层Alice占优势,只需左右区间中一者 ...