Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Line-break in a return statement causes "unreachable code" error #927

Closed
YetiCGN opened this issue Jan 21, 2016 · 4 comments
Closed

Line-break in a return statement causes "unreachable code" error #927

YetiCGN opened this issue Jan 21, 2016 · 4 comments

Comments

@YetiCGN
Copy link

YetiCGN commented Jan 21, 2016

This code validates fine:

return (this.intervalInMilliseconds >= 10000) && (this.intervalInMilliseconds <= 100000);

while this does not (error "unreachable code" after the semicolon):

return
    // Validate minimum interval
    (this.intervalInMilliseconds >= 10000)
    &&
    // Validate maximum interval
    (this.intervalInMilliseconds <= 100000)
;

Additionally, the error is also caused by simply adding a line break after return.

@adidahiya
Copy link
Contributor

thanks for the bug report. we're open to PRs here, but unreachable code detection is coming to the compiler in 1.8 and we're going to deprecate it in TSLint soon (#661), so it's low priority.

@andy-hanson
Copy link
Contributor

The code is in fact unreachable. The addition of the line break puts the return on its own line, so you return undefined.

@jkillian
Copy link
Contributor

Good insight @andy-hanson! Indeed, TSLint is giving you a very helpful warning here @YetiCGN, this article on semicolon insertion talks a little bit about similar situations with return and other constructs.

@YetiCGN
Copy link
Author

YetiCGN commented Jan 27, 2016

Thanks for the clarification! I was not aware of this particular quirk in TS/JS, since I'm used to writing return statements like this from other languages. It didn't even compile, so I had no chance to see the error in JS. ;)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants