TypeScript never类型的目的

TypeScript never类型的目的

类型脚本 是一种类型严格的语言,我们需要为每个变量定义类型。此外,我们还需要定义函数的返回类型和函数的参数类型。

在TypeScript中,never也是一种类型,就像其他数据类型,如字符串、数字、布尔值、符号等。我们可以使用’never’关键字来使一个变量成为never类型。

当用户确信任何情况都不会发生时,可以使用never类型。例如,当我们确定函数永远不会返回任何值时,我们可以使用never作为返回类型。

语法

用户可以按照下面的语法来使用never类型作为变量的字面意思。

// never type as a variable literal
let variable: never;

// never type as a function return type
function func(): never {

   // write any condition such that the function shouldn't return any value
}

在上面的语法中,变量类型是never,这意味着我们永远不能向该变量存储任何值。另外,函数的返回类型是never,这意味着我们永远不能从函数中返回值。

例子1:将never关键字作为一个字面意思使用

在下面的例子中,我们定义了变量,并将never关键字作为字面意思使用。之后,用户可以看到,我们不能给never类型的变量赋值。如果我们试图给never类型的变量赋值,TypeScript编译器会产生一个错误。

// never type as a variable literal
let variable: never;

// The below code generates the error message like Type 'number' is not assignable to type 'never'. 
// variable = 10;

// we can't print the variable before initializing it.
// console.log(variable);

// This will not work as we can't assign the value to the variable of never type
// let variable2: never = 10;

console.log("Working the variable of type never")
// never type as a variable literal
var variable;

// The below code generates the error message like Type 'number' is not assignable to type 'never'. 

// variable = 10;

// we can't print the variable before initializing it.

// console.log(variable);

// This will not work as we can't assign the value to the variable of never type

// let variable2: never = 10;
console.log("Working the variable of type never");

上述代码将产生以下输出 —

输出

Working the variable of type never

例2:使用 “never “作为函数的返回类型

在下面的例子中,我们已经创建了样本和测试函数。样本函数无限次地运行while循环,它永远不会停止。测试函数也是无限次地运行for循环,所以它永远不会返回值,我们可以使用never作为函数的返回类型。

另外,用户可以看到,样本()函数永远不会被TypeScript调用,因为测试()函数的执行永远不会停止。

// defining the sample function, which never returns a value and never stops execution
function sample(param1: string): never {
   while (true) {
      console.log("The value of the param1 is " + param1);
   }
}

// test function, which will return for loop for infinite time
function test(param2: number): never {
   for (let i = 0; ; ) {
      console.log("The value of param2 is " + param2);
   }
}

// calling the test function
test(20);

// calling the sample function

// sample function never invoked, as the test function will never stop
sample("Hello");
// defining the sample function, which never returns a value and never stops execution
function sample(param1) {
    while (true) {
      console.log("The value of the param1 is " + param1);
   }
}

// test function, which will return for loop for infinite time
function test(param2) {
   for (var i = 0;;) {
      console.log("The value of param2 is " + param2);
   }
}

// calling the test function
test(20);

// calling the sample function

// sample function never invoked, as the test function will never stop
sample("Hello");

示例 3

在这个例子中,我们已经创建了错误函数,它总是抛出一个错误。因此,它将永远不会从函数中返回任何值。之后,我们从sample()函数中调用了error()函数。

在输出中,用户可以观察到下面的代码显示了一个错误。

//  function that always throws an error
function error(): never {
   throw new Error("Errors in the return type!");
}

// calling the error() function from the sample() function
function sample() {
   return error();
}

sample();
//  function that always throws an error
function error() {
   throw new Error("Errors in the return type!");
}

// calling the error() function from the sample() function
function sample() {
   return error();
}
sample();

上述代码将产生以下输出 —

输出

Error: Errors in the return type!

使用从来没有的类型作为一个联盟或与其他数据类型的交叉点

我们可以将never类型作为一个字面,与其他数据类型如数字、字符串或布尔型一起使用。如果我们把never类型和数字类型联合起来,那么这个变量就变成了数字类型。

如果我们采取从不类型和数字类型的交集,从不类型总是覆盖,而变量成为从不类型。

语法

用户可以按照下面的语法来使用从来类型作为另一个数据类型的联合和相交。

let var1: never | number;
let var2: never & number;

在上述语法中,var1是never或number类型,而var2是never和number的类型,这意味着never类型总是覆盖number类型。

示例 4

在下面的例子中,用户可以看到,我们可以为var1赋值,因为数字类型覆盖了never类型。同时,我们不能给var2赋值,因为never类型覆盖了数字数据类型。

let var1: never | number;
let var2: never & number;

// we can assign value to the var1 as it is a type of number
var1 = 30;

//  we can't assign value to the var2 as it is a type of never
// var2 = 30;

console.log("The value of var1 is " + var1);
var var1;
var var2;
// we can assign value to the var1 as it is a type of number
var1 = 30;

//  we can't assign value to the var2 as it is a type of never

// var2 = 30;
console.log("The value of var1 is " + var1);

上述代码将产生以下输出 —

The value of var1 is 30

用户在本教程中了解了TypeScript中的never type。我们已经通过不同的例子看到了never类型的不同使用情况。当我们使用never作为函数的返回类型时,函数应该包含任何条件,这些条件永远不允许函数返回一个值。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程