空类型
一句话解释
空类型 indicates 那个函数's 返回值 应该不 是 used. 它是 返回类型 for 函数那个 don't 返回任何事 meaningful.
用大白话说
void type means " 返回值的这个函数应该是 ignored." It is used for functions that perform an action but don't produce a result, like event handlers and logging functions.
空 differs from undefined: a 空 function can return anything (the value is just ignored), while an undefined function must specifically return undefined. This matters for callbacks — a 空-returning callback can return any type.
You rarely write 空 yourself — it is usually inferred for functions without a return statement. It is most useful for typing callback parameters where the return value is ignored.
示例
typescript
function logMessage(msg: string): void {
console.log(msg);
// no return statement
}
// Callback with void return type
function forEach<T>(arr: T[], cb: (x: T) => void): void {
arr.forEach(cb);
}空平均值返回值 是 ignored. 回调使用空 能返回任何事.
相关术语
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。