最佳答案
In TypeScript, you can annotate a function as returning void:
function fn1(): void {
// OK
}
function fn2(): void {
// Error
return 3;
}
You can also annotate a function to return undefined:
function fn3(): undefined {
// OK
return;
}
function fn4(): undefined {
// Error
return 3;
}
So it seems that if you call a function returning void, you'll always get back the value undefined. Yet you can't write this code:
function fn5(): void {
}
let u: undefined = fn5(); // Error
Why isn't void just an alias for undefined? Does it need to exist at all?