[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 ...
随机推荐
- OC - 正则表达式 - RegexKitLite
正则表达式使用步骤: 1. 创建正则表达式对象, 设置约束条件; NSString *pattern = @"\\d{1,3}"; NSRegularExpression *reg ...
- OC修饰词 - 内存管理
<招聘一个靠谱的 iOS>—参考答案(上) 说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外,其他54道均 ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件
一.代码 1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...
- 循环冗余校验(CRC)算法入门引导
目录 写给嵌入式程序员的循环冗余校验CRC算法入门引导 前言 从奇偶校验说起 累加和校验 初识 CRC 算法 CRC算法的编程实现 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌 ...
- 【Xamarin挖墙脚系列:最重要的布局ListView】
原文:[Xamarin挖墙脚系列:最重要的布局ListView] 安卓的几个重要的布局 线性布局 相对布局 Table布局 Tab布局 表格Grid布局 列表布局. 这几种基本的布局的方式,最重要 ...
- ruby hashtable散列表
dict={'cat'=>'abc','dog'=>'def'}puts dict.size dict.keys返回所有的key, values返回所有的value. 删除: dict.d ...
- POJ_2739_Sum_of_Consecutive_Prime_Numbers_(尺取法+素数表)
描述 http://poj.org/problem?id=2739 多次询问,对于一个给定的n,求有多少组连续的素数,满足连续素数之和为n. Sum of Consecutive Prime Numb ...
- 计数方法,博弈论(扫描线,树形SG):HDU 5299 Circles Game
There are n circles on a infinitely large table.With every two circle, either one contains another o ...
- 统计难题 HDOJ --1251
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...