typescript - typescript 中的参数 'void' 是什么意思?

标签 typescript

我知道“void”类型在 typescript 上的作用。但我遇到了以下代码。

function (obj: void){}

我在 typescript 文档中看到了同样的内容 https://www.typescriptlang.org/docs/handbook/functions.html

输入“void”是什么意思 功能参数?

最佳答案

void 作为 this 类型很有用,因为它 promise 您不会使用 this 参数。

function f(this: void, ...) {
  // Can't use `this` in here.
}

在其他参数上,它……没那么有用。如果您关闭了 --strictNullChecks,那么您仍然可以通过将 nullundefined 作为 void 参数传递来调用该函数。如果不这样做,那么您甚至无法调用该函数,因为 void 无人居住。

如果你以前没有见过this写成函数参数,我建议你阅读this section (完全是双关语)文档。

关于typescript - typescript 中的参数 'void' 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54280047/

相关文章:

javascript - 使用 Github SVG 创建自定义 mat-icon

git - 存储库不干净

javascript - Angular 将 ViewChild 绑定(bind)到类中的属性

angular - 从头开始使用 Angular 2 的旋转木马/ slider

javascript - 让动画在 Angular 6 组件中不起作用

reactjs - 使用带有 typescript 的 ag-grid-react 的错误

typescript - 整数(文字)联合类型,以 NaN 作为有效返回值

angular - 错误 : Schema validation failed with the following errors: Data path "" should NOT have additional properties(project)

javascript - 只能从复选框事件访问一个类变量

javascript - Switch 语句不适用于某些变量