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的更多相关文章

  1. 从C#到TypeScript - function

    总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...

  2. 【TypeScript】如何在TypeScript中使用async/await,让你的代码更像C#。

    [TypeScript]如何在TypeScript中使用async/await,让你的代码更像C#. async/await 提到这个东西,大家应该都很熟悉.最出名的可能就是C#中的,但也有其它语言也 ...

  3. TypeScript入门七:TypeScript的枚举

    关于枚举 数字枚举 字符串枚举 异构枚举 计算的和常量成员 运行时的枚举与反向映射 常量枚举与外部枚举 一.关于枚举 枚举:一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计 ...

  4. TypeScript入门三:TypeScript函数类型

    TypeScript函数类型 TypeScript函数的参数 TypeScript函数的this与箭头函数 TypeScript函数重载 一.TypeScript函数类型 在上一篇博客中已经对声明Ty ...

  5. TypeScript入门五:TypeScript的接口

    TypeScript接口的基本使用 TypeScript函数类型接口 TypeScript可索引类型接口 TypeScript类类型接口 TypeScript接口与继承 一.TypeScript接口的 ...

  6. 转载:TypeScript 简介与《TypeScript 中文入门教程》

    简介 TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.安德斯·海尔斯伯格,C#的首席架构 ...

  7. [TypeScript] JSON对象转TypeScript对象范例

    [TypeScript] JSON对象转TypeScript对象范例 Playground http://tinyurl.com/nv4x9ak Samples class DataTable { p ...

  8. [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 ...

  9. 001——Typescript 介绍 、Typescript 安 装、Typescript 开发工具

    一. Typescript 介绍 1. TypeScript 是由微软开发的一款开源的编程语言. 4. TypeScript 是 Javascript 的超级,遵循最新的 ES6.Es5 规范.Typ ...

随机推荐

  1. xcode 升级插件失效问题

    摘要  Xcode 升级到7之后VVDocumenter-Xcode,OMColorSense,KSImageNamed等一系列的插件失效的解决办法,以及不小心误点了 Skipbundle 的解决办法 ...

  2. struts2简介

    MVC框架.不过有一点需要注意的是:struts2和struts2虽然名字很相似,但是在两者在代码编写风格上几乎是不一样的.那么既然有了struts1,为什么还要推出struts2.主要的原因是str ...

  3. PHP错误Warning: Cannot modify header information - headers already sent by解决方法

    这篇文章主要介绍了PHP错误Warning: Cannot modify header information - headers already sent by解决方法,需要的朋友可以参考下 今天在 ...

  4. 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...

  5. ANDROID_MARS学习笔记_S05_002_给传感器注册listener

    1 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); se ...

  6. Android 如何把一个 RelativeLayout或ImageView背景设为透明

    在项目中,需要把RelativeLayout 和  ImageView背景设置为透明,怎么实现呢?这里主要通过代码,请参阅以下关键代码: public ImageView imgDetail; pri ...

  7. Php使用sqlite

    php sqlite文档:http://php.net/manual/en/book.sqlite.php sql:http://www.php100.com/html/webkaifa/PHP/PH ...

  8. ARP 实现

    ARP 实现 现在我们介绍一下arp的实现,内核版本2.6.24. [数据结构] 协议栈通过ARP协议获取到的网络上邻居主机的IP地址与MAC地址的对应关 系都会保存在这个表中,以备下次与邻居通讯时使 ...

  9. 2821: 作诗(Poetize)

    2821: 作诗(Poetize) Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1078  Solved: 348[Submit][Status] ...

  10. 备份及还原Xcode的模拟器

    http://blog.csdn.net/it_magician/article/details/8749876 每次更新或者重装Xcode之后,最麻烦的莫过于各个模拟器的安装了,因为下载速度实在让人 ...