如何在TypeScript中创建条件类型

如何在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 中,接口的工作方式与类型别名相同,因为它定义了对象或类的结构。

之后,我们将 interface2 扩展为 interface1。这意味着 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()函数的返回类型进行了函数重载。我们可以观察到,如果参数类型是布尔型,返回类型是字符串。进一步地,如果参数类型是字符串或者数字,返回类型分别为数字和布尔型。

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

由于我们已经使用了条件类型作为变量的类型,我们可以将其用于函数的参数或返回类型。

在本教程中,用户学习了如何创建条件类型,这样我们就可以根据另一个变量的类型或值选择特定的变量。此外,我们还学习了如何将条件类型用于函数的参数和返回类型。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程