如何在 TypeScript 中创建条件类型?
在 TypeScript 中,我们需要为每个变量和对象定义类型,因为它是一种严格语言的类型,并且还包含条件类型。
从条件类型这个词,我们可以预测我们需要根据特定条件选择一个变量。是的,你没听错。正如我们使用 if-else 语句根据特定条件执行特定代码块一样,我们也可以根据特定条件选择变量的类型。
在本教程中,我们将学习在 TypeScript 中创建条件类型。
语法
用户可以按照以下语法在 TypeScript 中创建条件类型。
first_type extends second_type ? true_type : false_type;
我们在上述语法中使用三元运算符来创建条件类型。
操作数解释
First_type - 它是一个类型或变量。
Second_type - 它是一种类型,如数字、字符串、布尔值等。
True_type - 如果first_type包含second_type,true_type将被分配给变量。
False_type - 如果first_type不扩展second_type,则false_type将被分配给变量。
现在,我们将查看不同的示例,以了解有关 TypeScript 中条件类型的更多信息。
示例
在下面的示例中,我们定义了两个接口。在 TypeScript 中,接口的工作方式也与类型别名相同,因为它定义了对象或类的结构。
之后,我们用interface1扩展了interface2。这意味着interface2包含interface1的所有属性。之后,我们使用三元运算符将条件类型分配给 type1 和 type2 别名。
在输出中,用户可以检查 var1 和 var2 变量的类型。
// Creating the first interface interface interface1 { prop1?: string; prop2: boolean; } // creating the second interface and extending it with the interface1 interface interface2 extends interface1 { prop3?: number; prop4: boolean; } // type of the type1 is number as interface2 extends interface1 type type1 = interface2 extends interface1 ? number : string; let var1: type1 = 20; // type of the type2 is string as interface1 doesn't extends the interface2 type type2 = interface1 extends interface2 ? number : string; let var2: type2 = "Hello"; console.log("The type of var1 variable is " + typeof var1); console.log("The type of var2 variable is " + typeof var2);
编译时,它将生成以下 JavaScript 代码 -
var var1 = 20; var var2 = "Hello"; console.log("The type of var1 variable is " + typeof var1); console.log("The type of var2 variable is " + typeof var2);
输出
上述代码将产生以下输出 -
The type of var1 variable is number The type of var2 variable is string
我们已经学习了条件类型的基础知识以及如何创建它。
为什么使用条件类型?
我们将了解条件类型在现实开发中为何以及如何有用。
让我们看一下下面的代码,其中我们通过根据参数类型更改其返回类型来重载 func1() 函数。我们可以观察到,如果参数类型是boolean,则返回类型是字符串。另外,如果参数类型为字符串和数字,则返回类型分别为数字和布尔值。
function func1(param1: boolean): string; function func1(param1: string): number; function func1(param1: number): boolean; function func1(param1: any): any { // function body of the overloaded function }
我们可以通过在一行中使用一个定义创建条件类型来重载该函数,而不是在函数中编写多个定义。
示例
在下面的示例中,我们创建了名为 test_type 的条件类型。它获取值并根据值类型返回类型。如果值的类型是数字,则返回布尔值;对于字符串值,它返回一个数字,对于布尔值,它返回字符串类型。
在输出中,我们可以观察从 test_type 获得的变量和 abc 变量的类型。
// creating the conditional type // it will accept the number, string, and boolean values type test_type<T extends number | string | boolean> = T extends number ? boolean : T extends string ? number : string; // getting the type of abc variable based on the value from the conditional test_type let abc: test_type<"hello"> = 20; console.log("The type of the variable abc is " + typeof abc); let variable: test_type<typeof abc> = false; console.log("The type of the variable is " + typeof variable);
编译时,它将生成以下 JavaScript 代码:
// getting the type of abc variable based on the value from the conditional test_type var abc = 20; console.log("The type of the variable abc is " + typeof abc); var variable = false; console.log("The type of the variable is " + typeof variable);
输出
上述代码将产生以下输出 -
The type of the variable abc is number The type of the variable is boolean
由于我们已经对变量使用了条件类型,因此我们可以将其用于函数参数或返回类型。
用户在本教程中学习了创建条件类型,使我们能够根据另一个变量的类型或值选择特定变量。此外,我们还学习了如何使用函数参数和返回类型的条件类型。
以上是如何在 TypeScript 中创建条件类型?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

如何在JavaScript中将具有相同ID的数组元素合并到一个对象中?在处理数据时,我们常常会遇到需要将具有相同ID�...

学习JavaScript不难,但有挑战。1)理解基础概念如变量、数据类型、函数等。2)掌握异步编程,通过事件循环实现。3)使用DOM操作和Promise处理异步请求。4)避免常见错误,使用调试技巧。5)优化性能,遵循最佳实践。

实现视差滚动和元素动画效果的探讨本文将探讨如何实现类似资生堂官网(https://www.shiseido.co.jp/sb/wonderland/)中�...

深入探讨console.log输出差异的根源本文将分析一段代码中console.log函数输出结果的差异,并解释其背后的原因。�...

JavaScript的最新趋势包括TypeScript的崛起、现代框架和库的流行以及WebAssembly的应用。未来前景涵盖更强大的类型系统、服务器端JavaScript的发展、人工智能和机器学习的扩展以及物联网和边缘计算的潜力。
