-
Notifications
You must be signed in to change notification settings - Fork 87
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
feat/type-safe: Encourage type-safe coding practices with eslint-plugin-typesafe #943
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
liangyuanruo
changed the title
feat/type-safe: Prevent use of throw statements in synchronous functinons
feat/type-safe: Prevent use of throw statements in synchronous functions
Dec 27, 2020
liangyuanruo
changed the title
feat/type-safe: Prevent use of throw statements in synchronous functions
feat/type-safe: Encourage type-safe coding practices with eslint-plugin-typesafe
Dec 27, 2020
One can simply rely on type checking against the function return signature. For instance, type checks will fail with "Not all code paths return a value" in the case below: ```ts type MyNumber = 1 | 2 function test(x: MyNumber) { switch(x) { case 1: return 1 } } ```
liangyuanruo
force-pushed
the
feat/type-safe
branch
from
December 29, 2020 02:03
4fde8ae
to
1a41525
Compare
mantariksh
approved these changes
Jan 5, 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exciting, lgtm!
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
TypeScript offers a powerful static typing system that can help us avoid painful bugs. However, there are also limitations that prevent the code from being fully type-safe.
For example, there is no way to enforce error handling even with this function signature:
And, given a codebase of sufficient complexity, an unexpected error state will eventually occur that could bubble up to the entry point and crash the application. Because every valid JavaScript program must also be a valid TypeScript program, this problem is unlikely to ever be fixed by the compiler.
Solution
For a start, introduce a new linting rule named no-throw-sync-func from the new eslint-plugin-typesafe library, which prevents the use and further proliferation of throw statements within synchronous functions.
(full disclosure: I created the
eslint-plugin-typesafe
library during learning).Like all linters, the plugin works by pattern matching against the Abstract Syntax Tree (AST) representation of the code and reporting violations to the linter. The rule is smart enough to prevent the use of
throw
where we probably shouldn't be using it most of the time (e.g. synchronous function declarations, expressions, and arrow function expressions methods), but also allow its use when it is probably safe to use it (e.g. async functions, Promise chaining, constructors).As the team has already committed to using
neverthrow
for type safety, relatively few lines of code needed to be refactored to pass the new linting checks (with the exception of verification.service.ts). Code which ought to crash the application, such as bootstrapping code, were exempted from the linting rule.As this is only a
devDependency
,eslint-plugin-typesafe
does not ship to production.Future work
This rule only prevents
Errors
from throwing, but notUnhandledPromiseRejection
s. Future work would include additional rules to try and address this shortcoming.Exhaustive type checking in switch statements
Commits (such as 7d2509956898a41aa3400cd5c08f909ac5a714af) removes the need to use
assertUnreachable
in switch statements as one could simply rely on built-in exhaustive type checking against the argument type.For example, the compilation will fail with "Not all code paths return a value" in the case below:
Screenshots
Deploy Notes
New dev dependencies:
eslint-plugin-typesafe
- An ESLint plugin to encourage writing typesafe code