[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 they’re invoked. Using TypeScript’s function overloads, you can create an overload for each allowed combination of parameter and return types. This way, all type-correct signatures of a function are encoded in the type system and can be surfaced by the TypeScript Language Service within your editor.
/**
* Reverses the given string.
* @param string The string to reverse.
*/
function reverse(string: string): string; /**
* Reverses the given array.
* @param array The array to reverse.
*/
function reverse<T>(array: T[]): T[]; function reverse(stringOrArray: string | any[]) {
return typeof stringOrArray === "string"
? [...stringOrArray].reverse().join("")
: stringOrArray.slice().reverse();
} // [ts] const reversedString: string
const reversedString = reverse("TypeScript");
// [ts] const reversedArray: number[]
const reversedArray = reverse([, , , , , ]);
console.log(reversedString)
console.log(reversedArray)


[TypeScript] Overload a Function with TypeScript’s Overload Signatures的更多相关文章
- [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, bas ...
- [TypeScript] Define a function type
type DigitValidator = (char) => boolean; -]{}/.test(char); export const digitValidators: {[key: s ...
- [Typescript] Introduction to Generics in Typescript
If Typescript is the first language in which you've encountered generics, the concept can be quite d ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- 深入浅出TypeScript(2)- 用TypeScript创建web项目
前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack ...
- 学习TypeScript,笔记一:TypeScript的简介与数据类型
该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27 16:50:03 一.什么是TypeScript? TypeScript是javasc ...
- [TypeScript] Custom data structures in TypeScript with iterators
We usually think of types as something that can define a single layer of an object: with an interfac ...
- [Typescript] Specify Exact Values with TypeScript’s Literal Types
A literal type is a type that represents exactly one value, e.g. one specific string or number. You ...
- [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type
ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...
随机推荐
- Oracle11g 审计介绍
审计是记录数据库上方方面面操作.事件等信息,是数据安全管理的重要手段. 开启审计,虽然不同级别的审计会有不同,但是对数据库的性能是有影响的,并且占用存储空间. --1.创建审计数据专用表空间 crea ...
- Python 源码分析:queue 队列模块
起步 queue 模块提供适用于多线程编程的先进先出(FIFO)数据结构.因为它是线程安全的,所以多个线程很轻松地使用同一个实例. 源码分析 先从初始化的函数来看: 从这初始化函数能得到哪些信息呢?首 ...
- 升级openssh踩得坑
升级背景: 项目中使用的系统为CentOS6.8,经过漏洞扫描后发现openssh高危漏洞,具体描述如下:OpenSSH 7.2p2之前版本, sshd/ session.c/ do_setup_en ...
- Matplotlib中的颜色
使用matplotlib中会遇到选择颜色的问题,很多人会觉得自带的matlab风格的颜色不好看.好在Matplotlib已经预见到了这个问题,除了支持最基本的matlab传统颜色之外,还支持很多种颜色 ...
- 集训第四周(高效算法设计)B题 (二分查找优化题)
---恢复内容开始--- Description Before the invention of book-printing, it was very hard to make a copy of ...
- UVa 548 树(已知其中两种遍历, 还原树)
题意: 给出后序遍历和先序遍历, 还原一棵树, 然后求出从根节点到叶子的最小路劲和. 分析: 已知后序遍历, 那么后序的最后一个节点就是根节点, 然后在中序中找到这个节点, 它的左边就是左子树, 它的 ...
- JS逻辑运算符&&与||的妙用
JS逻辑运算符&&与||的妙用 /* 文章写的不错 就此分享 */ &&中第一个表达式为假就不会去处理第二个表达式,直接放回结果. || 中就刚很好相反.如果第一个 ...
- ..net 3.5新特性之用this关键字为类添加扩展方法
具体用法如下: public static class ClassHelper { //用this 声明将要吧这个方法附加到Student对象 public static bool CheckName ...
- IDEA-基本设置
目录: 1.设置内存 2.设置编码格式 3.设置换行符 4.设置新建Class文档说明 5.添加自定义注释 6.设置自己的maven 工欲善其事,必先利其器,设置好基础的设置才能事半功倍!少踩坑!以下 ...
- 7-26 Windows消息队列(25 分)(堆排序)
7-26 Windows消息队列(25 分) 消息队列是Windows系统的基础.对于每个进程,系统维护一个消息队列.如果在进程中有特定事件发生,如点击鼠标.文字改变等,系统将把这个消息加到队列当中. ...