[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([4, 8, 15, 16, 23, 42]);
console.log(reversedString)
console.log(reversedArray)
时间: 2024-10-21 05:14:12

[TypeScript] Overload a Function with TypeScript’s Overload Signatures的相关文章

[TypeScript] Define a function type

type DigitValidator = (char) => boolean; const numericValidator = (char) => /[0-9]{1}/.test(char); export const digitValidators: {[key: string]: DigitValidator} = { '9': numericValidator }; We can use 'type' keyword to define a function type. 'digit

[Typescript] Introduction to Generics in Typescript

If Typescript is the first language in which you've encountered generics, the concept can be quite difficult to understand. We skip the lecture in this lesson and dive straight into a real-world use-case that is guaranteed to help you understand the

[TypeScript] Understand lookup types in TypeScript

Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the property keys of an object. We'll use the function spyOn from Jest to illustrate how lookup types can type-safe function parameters. Considering this code:

[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 can combine literal types with union types to model a finite set of valid values for a variable. In this lesson, we explore the all kinds of literal t

Overload和Override的区别,Overload 的方法是否可以改变返回值类型?

Overload:重载,发生在同一个类中,各个方法名相同,方法参数的个数.顺序或类型不同:返回值类型不同或形参名称不同,不构成方法重载: Override:重写,发生继承关系中,子类所重写的方法与父类方法 方法名相同,参数个数.顺序和类型相同:返回值类型相同或者是 父类中的返回值类型的子类.子类不能重写父类的private(私有).static和final方法,子类中重写的方法不能抛出比父类更大的异常.构造方法不能被重写. 原文地址:https://www.cnblogs.com/remta/p

[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 to more accurately type methods such as Object.create. Don't confuse it with the Object type or {}, the empty object type, though! Type 'Object' gives

从C#到TypeScript - function

从C#到TypeScript - function 虽然TypeScript里有了类,但JavaScript的function也还在,这也是和C#的不同所在.C#里函数不能脱离类工作,但TypeScript的function和JavaScript一样,可以单独工作. 函数类型 函数和C#一样可以有名字,也可以是匿名函数,匿名函数有两种写法: function checkLogin(name: string, pwd: string): boolean{    return true; }let 

使用TypeScript拓展你自己的VSCode

转自:http://www.iplaysoft.com/brackets.html使用TypeScript拓展你自己的VSCode! 0x00 前言在前几天的美国纽约,微软举行了Connect(); //2015大会.通过这次大会,我们可以很高兴的看到微软的确变得更加开放也更加务实了.当然,会上放出了不少新产品和新功能,其中就包括了VS Code的beta版本.而且微软更进一步,已经在github将VS Code的代码开源了.除了这些让人兴奋的消息,我们还应该注意到VS Code提供了对拓展的支

TypeScript和JavaScript哪种语言更先进

TypeScript和JavaScript哪种语言更先进 近两年来最火爆的技术栈毫无争议的是JavaScript,随着ES6的普及,不管是从前端的浏览器来看,还是后端的NodeJS场景,JavaScript技术栈不断的向世界证明自己的价值.JavaScript代码越写越大,众所周知,JavaScript是一门动态语言,缺少静态类型检查,这样就很难在编译阶段排除更多的问题,当然,这就是动态语言的魅力所在,运行时动态处理类型,在我们写代码的时候就可以很更灵活.为了给JavaScript增加类型检查以