Must return a value
A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.The compiler’s own words. Not translated — this is the string you pasted into a search box.
The signature promises something comes back and the body never delivers it.
Reproduction
function f(): number {
}The build asserts this emits exactly this code.
Why the compiler says this
A return type is a promise to every caller, and this error is the compiler holding you to it before anyone relies on it. The message names the three types that are allowed to mean "nothing comes back", which is also a useful reminder that `void` and `undefined` are not the same thing: `void` says the caller should ignore the result, `undefined` says the result is a real value that happens to be undefined.
Fixes
- 01
function f(): number { return 1 }Keep the promise.
- 02
function f(): void { }Or stop making it. `void` is the right return type for a function that exists for its effects, and saying so is better than returning something nobody wants.
Takeaway
This error also fires when only *some* paths return. If it surprises you, look for the branch that falls off the end.