[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 ...
随机推荐
- 出现“error c4430缺少类型说明符-假定为int。注意C++不支持默认int
出现这种错误的原因,是因为函数没有写返回值.是在VC6.0的工程转为高版本(VS2010)的时候经常出现的; #include <stdio.h> main() { printf(&quo ...
- oracle 一行转多行
比如sql: select zyxdm from table where bindid=2265254 查询结果为:1|4|8|9|10 将这个查询结果转成多行,结果如下: ID 1 4 8 9 10 ...
- 6.memcached缓存系统
1.memcached的安装和参数 memcached缓存系统一般还是部署在linux服务器上,所以这里只介绍linux上memcache的安装 首先切换到root用户,然后apt-get insta ...
- [ nginx ] 带宽下载限速
nginx上了一个APP提供给用户下载,考虑到带宽占用的问题,决定在nginx上做下载限速处理. 操作系统:Centos6.7 X64 nginx版本:nginx/1.11.3 根据官方文档: 对ng ...
- java连接Fastdfs图片服务器上传失败的解决方法
照着视频上做,但是却连接不了虚拟机linux上的图片服务器,估计是linux防火墙的问题(这个实在是神烦,前面有好几次连接不了都是因为linux防火墙),果不其然,关闭即可. Linux关闭防火墙的命 ...
- ipython notebook install
1.python install (ubuntut系统默认2.7.x) Github: https://github.com/ipython/ipython 2.sudo apt-get instal ...
- LCA+差分【p4427】[BJOI2018]求和
Description master 对树上的求和非常感兴趣.他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的\(k\) 次方和,而且每次的\(k\) 可能是不同的.此处节点深度的 ...
- oracle null 相关的另外2个方法
- centos 7下查找大文件、大目录和常见文件查找操作
根据园子 潇湘隐者的文章 <Linux如何查找大文件或目录总结>结合实际运维需要整理出常用命令 目标文件和目录查找主要使用 find 命令 结合 xargs (给命令传递参数的一个过滤器, ...
- 子域名/目录暴力工具Gobuster
子域名/目录暴力工具Gobuster Gobuster是Kali Linux默认安装的一款暴力扫描工具.它是使用Go语言编写的命令行工具,具备优异的执行效率和并发性能.该工具支持对子域名和Web目 ...