什么是';无效';作为typescript中的参数意味着什么?

什么是';无效';作为typescript中的参数意味着什么?,typescript,Typescript,我知道类型“void”在typescript上的作用。但我面临以下代码 function (obj: void){} 我在typescript文档中也看到了同样的情况 “void”类型是什么 乐趣参数?void作为this类型很有用,因为它保证您不会使用this参数 function f(this: void, ...) { // Can't use `this` in here. } 在其他参数上,它是。。。没有那么有用。如果禁用了--stricnullchecks,则仍然可以通过将n

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

function (obj: void){}
我在typescript文档中也看到了同样的情况

“void”类型是什么
乐趣参数?

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

function f(this: void, ...) {
  // Can't use `this` in here.
}
在其他参数上,它是。。。没有那么有用。如果禁用了
--stricnullchecks
,则仍然可以通过将
null
undefined
作为void参数来调用函数。如果没有,那么您甚至不能调用该函数,因为
void
是无人居住的


如果您以前没有看到过作为函数参数编写的
,我建议您阅读(完全是双关语)文档。

简而言之,
void
用于表示缺少值。您可以将其视为表示
未定义的另一种方式

const foo: void = undefined;
function log(argument: any): void {
    console.log(argument);
}

const foo: undefined = log('Hello'); // Error — "void" is not "undefined"
当用作返回类型时,
void
表示函数不会显式返回任何内容

function log(argument: any): void {
  console.log(argument);
}
虽然在运行时
log
会隐式返回
未定义的
,但TypeScript在概念上区分了
无效的
未定义的

const foo: void = undefined;
function log(argument: any): void {
    console.log(argument);
}

const foo: undefined = log('Hello'); // Error — "void" is not "undefined"
当用作
this
类型时,
void
表示函数调用期间使用的
this
将是默认执行上下文-全局范围。在一些情况下,它会有所帮助。创建就是其中之一。

您仍然可以调用一个
(a:void)=>void
,而不使用任何参数或
任何