noImplicitReturns
Every path or none
A function where some branches return a value and others fall off the end is almost always a missing case.
- Since
- TypeScript 2.0
- strict
- Not in strict
- In your tsconfig
"noImplicitReturns": true
The same snippet both times. Only the option changed.
With it off
"noImplicitReturns": falseexport function grade(n: number) { if (n > 50) { return 'pass' } }
Compiles clean
With it on
"noImplicitReturns": trueexport function grade(n: number) { if (n > 50) { return 'pass' } }
Emits TS7030
Why the compiler bothers
Without an annotation the compiler infers `string | undefined` and everything downstream inherits the hole, so the mistake is reported — if it is reported at all — somewhere far from where it was made. This flag reports it here. It is about the shape of the function rather than the type of the result, which is why it catches the case an explicit return type would also catch and the case where you never wrote one.
Takeaway
An exhaustive switch that returns from every case satisfies it without a final return.
Where to go next
22 options

