Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code section below a never expression should be marked as unreachable #28859

Closed
5 tasks done
KSXGitHub opened this issue Dec 5, 2018 · 4 comments
Closed
5 tasks done
Labels
Experience Enhancement Noncontroversial enhancements

Comments

@KSXGitHub
Copy link
Contributor

KSXGitHub commented Dec 5, 2018

Suggestion

According to the TypeScript handbook:

The never type represents the type of values that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns; Variables also acquire the type never when narrowed by any type guards that can never be true.

Which means that function or expression that "returns" never either terminates the program prematurely or never return (i.e. infinite loop), this leads to the section of code below it never get to execute.

Examples

console.log('before') // reachable
process.exit(0) // this function returns `never` in @types/node 10.12.0
console.log('after') // should be marked as unreachable

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Related issues

#12825

@jack-williams
Copy link
Collaborator

Limited by the same issue here: #8655

Specifically, see this comment here.

Reachability in your example is determined by type, but the graph is built syntactically.

@ajafff
Copy link
Contributor

ajafff commented Dec 5, 2018

I have a lint rule that forces you to write return process.exit(0) instead, which is then picked up by control flow analysis and subsequent statements are marked as unreachable: https://github.com/fimbullinter/wotan/blob/master/packages/mimir/docs/return-never-call.md

The CLI documentation contains a quick start guide: https://github.com/fimbullinter/wotan/blob/master/packages/wotan/README.md

@weswigham weswigham added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Dec 5, 2018
@MartinJohns
Copy link
Contributor

This seems to be implemented, so the Design Limitation tag can be removed @weswigham.

@weswigham weswigham added Experience Enhancement Noncontroversial enhancements and removed Design Limitation Constraints of the existing architecture prevent this from being fixed labels Nov 3, 2019
@weswigham
Copy link
Member

Added in 3.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements
Projects
None yet
Development

No branches or pull requests

5 participants