首页 > web前端 > js教程 > 正文

如何在 TypeScript 中创建条件类型?

WBOY
发布: 2023-08-23 18:33:11
转载
1011 人浏览过

如何在 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中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!