我正在学习打字稿,并且在教程中遇到了这个switch语句。我知道该数字已分配给a,因此所有参数都应为数字。但是,无效意味着什么?
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);
这意味着该函数不期望返回值,这在没有return
语句的事实中很明显。换句话说,函数只能等于null
或undefined
。
https://www.typescriptlang.org/docs/handbook/basic-types.html#void
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句