typescript - 打字稿中的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);
解决方案
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
推荐阅读
- angular - 错误类型错误:_co.function 不是函数
- r - ValueError:输入 0 与层 lstm_2 不兼容:预期 ndim=3,发现 ndim=4 - 多元时间序列数据
- javascript - 找到最近的#loading 并显示它?
- excel - 检查 B 列是否包含 A 列中存在的特定字符串,然后将其从 B 列的单元格中删除
- javascript - 如何更改 imageCapture.takePhoto() 的大小?
- pdf - 使用 Poppler 拆分和重新加入 PDF 会导致文件更大?(尽管剥离元数据)
- sql - 在不使用临时表的情况下将两个查询合二为一
- ios - ViewWillAppear 不调用模态视图控制器的解雇
- java - Spring MVC 和 Thymeleaf:如何在迭代后获取列表对象的值
- python - 使用 requests.get 调用 API 后,如何从返回的 JSON 对象中替换键的值?