[TypeScript] Function Overloads in Typescript
It's common in Javascript for functions to accept different argument types and to also return different types. In this lesson we learn how to 'teach' Typescript about these dynamic functions so that we can still benefit from the powerful static type analysis.
const getTasks = (taskname: string, x: any) : any => {
if(typeof x === 'function'){
return {taskname, fn: x};
} if(Array.isArray(x)){
return {taskname, deps: x};
}
}; const task1 = getTasks('taskname1', ['dep1', 'dep2']);
const task2 = getTasks('taskname2', function(){}); task1.fn(); // Runtime Error, fn not exists on task1
task2.deps; // Runtime Error, deps not exists on task2
In the code above, the IDE cannot help much during the compile time.
But if we use Function overloads, then IDE can help to check error:
interface GroupTask {
taskname:string
deps:string[]
} interface Task {
taskname:string
fn:Function
} function getTasks(taskname:string, x:string[]):GroupTask
function getTasks(taskname:string, x:Function):Task
function getTasks(taskname:string, x:any):any {
if (typeof x === 'function') {
return {taskname, fn: x};
} if (Array.isArray(x)) {
return {taskname, deps: x};
}
}
const task1 = getTasks('taskname1', ['dep1', 'dep2']);
const task2 = getTasks('taskname2', function () {
}); task1.fn // Property 'fn' doesn't not exist on type 'GrouptTask'
task2.deps // Property 'deps' doesn't not exist on type 'Task'
[TypeScript] Function Overloads in Typescript的更多相关文章
- 从C#到TypeScript - function
总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...
- 【TypeScript】如何在TypeScript中使用async/await,让你的代码更像C#。
[TypeScript]如何在TypeScript中使用async/await,让你的代码更像C#. async/await 提到这个东西,大家应该都很熟悉.最出名的可能就是C#中的,但也有其它语言也 ...
- TypeScript入门七:TypeScript的枚举
关于枚举 数字枚举 字符串枚举 异构枚举 计算的和常量成员 运行时的枚举与反向映射 常量枚举与外部枚举 一.关于枚举 枚举:一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计 ...
- TypeScript入门三:TypeScript函数类型
TypeScript函数类型 TypeScript函数的参数 TypeScript函数的this与箭头函数 TypeScript函数重载 一.TypeScript函数类型 在上一篇博客中已经对声明Ty ...
- TypeScript入门五:TypeScript的接口
TypeScript接口的基本使用 TypeScript函数类型接口 TypeScript可索引类型接口 TypeScript类类型接口 TypeScript接口与继承 一.TypeScript接口的 ...
- 转载:TypeScript 简介与《TypeScript 中文入门教程》
简介 TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.安德斯·海尔斯伯格,C#的首席架构 ...
- [TypeScript] JSON对象转TypeScript对象范例
[TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...
- [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)
This lesson shows you how to install TypeScript and run the TypeScript compiler against a .ts file f ...
- 001——Typescript 介绍 、Typescript 安 装、Typescript 开发工具
一. Typescript 介绍 1. TypeScript 是由微软开发的一款开源的编程语言. 4. TypeScript 是 Javascript 的超级,遵循最新的 ES6.Es5 规范.Typ ...
随机推荐
- 【转】centOS中mysql一些常用操作
安装mysql yum -y install mysql-server 修改mysql配置vi /etc/my.cnf 这里会有很多需要注意的配置项,后面会有专门的笔记暂时修改一下编码(添加在密码下方 ...
- APP 如何适应 iPhone 5s/6/6Plus 三种屏幕的尺寸
初代iPhone 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone 3GS也保持不变. 那时编写iOS的App( ...
- 什么是xsi:type ???
http://www.w3.org/2001/XMLSchema-instance http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.ecli ...
- DM8168 编译filesystem步骤
在板子跑起来之前,需要先编译好8168的文件系统.前提是已经设置好板子的类型等参数,详见<DM8168环境搭建> 1.进入<DVR_RDK_BASE>/dvr_rdk目录 ma ...
- http://www.cnblogs.com/xdp-gacl/p/3951952.html
http://www.cnblogs.com/xdp-gacl/p/3951952.html http://www.cnblogs.com/kristain/articles/2409021.html
- Java序列化与Hessian序列化的区别
Java序列化: Java序列化会把要序列化的对象类的元数据和业务数据全部序列化为字节流,而且是把整个继承关系上的东西全部序列化了.它序列化出来的字节流是对那个对象结构到内容的完全描述,包含所有的信息 ...
- Python自然语言处理学习笔记(69)
http://www.cnblogs.com/yuxc/archive/2012/02/09/2344474.html Chapter8 Analyzing Sentence Structure ...
- TeeChart的最小步长和最大步长
/// <summary> /// 坐标轴的最大步长 /// </summary> public double MajorStep { get { return axis.Ca ...
- Centos6.5 安装Vim7.4
系统本身会带Vim7.2都版本,其实也够用,强迫症患者可以按以下操作升级成Vim7.4: (1)切换到root权限 (2)卸载 rpm -qa | grep vim yum remove vim vi ...
- Python正则匹配多行,多个数据
最近用Python做一个crawler工具的时候,发现用一个正则表达式可以匹配到个数据的时候用match.group()只能打印出第一个数据,其它数据不能打印出来.最后找到解决方法,现在记录一下,直接 ...