[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 ...
随机推荐
- matlab中 hold on 与hold off的用法
matlab中 hold on 与hold off的用法 hold on 是当前轴及图形保持而不被刷新,准备接受此后将绘制 hold off 使当前轴及图形不在具备被刷新的性质 hold on 和ho ...
- hdu 1116
欧拉回路,利用并查集来实现: 代码: #include<cstdio> #include<cstring> #include<vector> using names ...
- Jquery IE 缓存问题
jQuery IE缓存问题 解决方法: 1.在AJAX请求的页面后加个随机函数,我们可以使用随机时间函数 在javascript发送的URL后加上t=Math.random() 例如这样:URL+”& ...
- SPRING IN ACTION 第4版笔记-第一章-002-DI介绍
一. 1.knight.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...
- Qt的事件模型(5种使用办法,通常重新实现event handler即可。只有定义控件才需要管理信号的发射)
Qt的事件模型 1.事件的概念 应用程序对象将系统消息接收为 Qt 事件.应用程序可以按照不同的粒度对事件加以监控.过滤并做出响应. 在 Qt 中,事件是指从 QEvent继承 的对象.Qt将事件发送 ...
- 【Xamarin挖墙脚系列:Xamarin的核心】
原文:[Xamarin挖墙脚系列:Xamarin的核心] Xamarin 包含两个商业产品 :Xamarin.IOS, Xamarin.Android.他们都是通过开源的基于.Net的Mono项目构建 ...
- 17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files
17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files 如果数据库是大的, 复制raw 数据文 ...
- python 替换windows换行符为unix格式
windows 默认换行符为 \r\n; unix默认换行符为 \n; 所以当win下编辑的脚本在linux下显示末尾多了^M: 换行符修改为同一的unix格式脚本如下: def run(path,f ...
- String.format 细节
String.format(Locale.US, format, args); format 参数 如果有% 那么%后面必须跟一个合法的字符,否则崩溃, 因为在String.format中 %为特殊字 ...
- WCF Concurrency (Single, Multiple, and Reentrant) and Throttling
http://www.codeproject.com/Articles/89858/WCF-Concurrency-Single-Multiple-and-Reentrant-and Introduc ...