[TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript
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的更多相关文章
- 深入浅出TypeScript(5)- 在React项目中使用TypeScript
前言 在第二小节中,我们讨论了利用TypeScript创建Web项目的实现,在本下节,我们讨论一下如何结合React创建一个具备TypeScript类型的应用项目. 准备 Webpack配置在第二小节 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [TypeScript] Dynamically initialize class properties using TypeScript decorators
Decorators are a powerful feature of TypeScript that allow for efficient and readable abstractions w ...
- [TypeScript] Define a function type
type DigitValidator = (char) => boolean; -]{}/.test(char); export const digitValidators: {[key: s ...
- Java中的Union Types和Intersection Types
前言 Union Type和Intersection Type都是将多个类型结合起来的一个等价的"类型",它们并非是实际存在的类型. Union Type Union type(联 ...
- 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 ...
随机推荐
- Redis安装-CentOs7
官方地址 确保gcc已经安装 $ yum list installed | grep gcc $ yum install gcc 下载.提取和编辑Redis: $ wget http://downlo ...
- libev 学习使用
libev 简单的I/O库. a high performance full featured event loop written in c libev 的大小也比 libevent 小得多并且自 ...
- css项目列表如何水平放置
列表项目默认分行排列,那么将列表项设置浮动就可以实现水平放置 1 li{float:left;} 示例如下: 创建Html元素 1 2 3 4 5 6 <ul> <li> ...
- ajax按钮改变数据状态
1.html代码 <td> @if($project->done_deal==) <button type="button" class="btn ...
- php 单文件上传
php 单文件上传 通过 PHP,可以把文件上传到服务器. 创建一个文件上传表单 允许用户从表单上传文件是非常有用的. 请看下面这个供上传文件的 HTML 表单: Filename: 请留意如下有关此 ...
- Java-事务管理
1.事务的概念: 事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功. 2. 管理事务: 2.1. 数据库默认的事务 数据库默认支持事务的,但是数据库默认的事务是一条sql语 ...
- Servlet 调用过程
上图的大概意思: 前台输入访问路径后,浏览器会去访问本地的host文件查询有木有与之匹配域名的IP地址,若无则在访问DNS服务器查询与之匹配的IP地址.解析IP后则开始发起HTTP请求,根据请求中的基 ...
- ubuntu下配置ProFtpd服务使用sqlite3作为后端用户认证
个人机器需要开个文件共享,Linux机器懒得配置SMB,就直接安装了ProFtpd,以做FTP服务器 Ubuntu安装挺简单,可使用就不那么友好了,配合GAdmin-Proftpd,一样不好用. 首先 ...
- Codeforces 811 C. Vladik and Memorable Trip
C. Vladik and Memorable Trip time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- HDU 1686 Oulipo【kmp求子串出现的次数】
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...