[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 ...
随机推荐
- Python_练习_VS清理器
#导入os import os #创建列表放入后缀 d=[ '.txt','obj','tlog','lastbuildstate','idb','pdb','pch','res','ilk','sd ...
- COMMIT - 提交当前事务
SYNOPSIS COMMIT [ WORK | TRANSACTION ] DESCRIPTION 描述 COMMIT 提交当前事务. 所有事务的更改都将为其他事务可见,而且保证当崩溃发生时的可持续 ...
- 怎样从SpringMVC返回json数据
Srping3中配置 maven依赖pom.xml 需要jackson库的依赖 <dependency> <groupId>org.codehaus.jackson</g ...
- Tab键可访问的下拉菜单demo
<ul id="navigationRegion"> <li token="1" class="index_on"> ...
- jsMap地图网点
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...
- 利用filter过滤去重
var r, ary = ['apple', 'strawberry', 'banana', 'pear', 'apple', 'orange', 'orange', 'strawberry']; r ...
- 零基础入门学习Python(17)--函数:Python的乐高积木
前言 相信大家小时候都玩过神奇的乐高积木, 只要通过想象力和创造力我们可以拼凑很多神奇的东西,那么随着我们学习的深入,我们编写的Python代码也将日益增加,并且也越来越复杂, 所以呢,我们需要找寻一 ...
- buf.keys()
buf.keys() 返回:{Iterator} 创建并返回一个包含 Buffer 键名(索引)的迭代器. const buf = Buffer.from('buffer'); for (var ke ...
- python 多线程并发threading & 任务队列Queue
https://docs.python.org/3.7/library/concurrency.htmlpython程序默认是单线程的,也就是说在前一句语句执行完之前后面的语句不能继续执行先感受一下线 ...
- LeetCode(59)SPiral Matrix II
题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...