首页 > 解决方案 > 打字稿中的void是什么/意味着什么?

问题描述

我正在学习打字稿,我在教程中遇到了这个 switch 语句。我知道数字被分配给a,所以所有参数都应该是数字。但是,void 是什么意思和做什么?

function switchFunction(a: number): void {
    switch (a) {
        case 1:
            let variableInCase1 = "test";
            console.log(variableInCase1);
            break;
        case 2:
            let variableInCase2 = "test2";
            console.log(variableInCase2);
            break;
        default:
          console.log("Default");    
    }
}
switchFunction(1);
switchFunction(2);
switchFunction(3);

标签: typescript

解决方案


It means that the function is not expecting a return value, evident in the fact that there is no return statement. In other words, the function can only equate to null or undefined.

https://www.typescriptlang.org/docs/handbook/basic-types.html#void


推荐阅读