-
Notifications
You must be signed in to change notification settings - Fork 12k
Enforce stricter types via lint when in strict mode #17821
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
Comments
I personally think that it’s a good pattern to always add a return type even when it’s inferred. Thus, I am in for this changed even for applications. Let’s see what other team members think about this. |
@alan-agius4 Thanks for your answer. If you want to have a bigger discussion about strictness, I think the It is also great to:
Note that I know that, due to history, Angular code itself is not compliant with this rule, but it does not impact applications (I use |
Side note: I am not a fan of having have different lint rules in the same mono repo. I think they should be consistent which offers a better DX. |
Hi @cyrilletuzi, We discussed this during our last team meeting and we'd like to do the following:
For consistence the changes need to be applied in the root level tslint configuration. |
Hi @alan-agius4, PR done: #17834 I'm very happy of this move. I wasn't expecting you would agree to go that way, I think I was the first to propose to add a It's one of the common pitfall I see in Angular projects I audit. Even in small/medium projects, there is quickly hundreds or even thousands of places where the code is not type safe. Leading to the whole projects to be very unreliable, with bugs multiplying over time, refactorings being very risky, etc. And it is very difficult to come back from that (I've passed weeks on some projects to make them type safe). Next step would be that |
@cyrilletuzi, the end goal is indeed to make I agree that when not using TypeScript in strict mode, you will use only a very small fraction of the potential of the TS type-checker which will lead to bugs which could be caught at an earlier stage. Thanks for your PR bdw 😁 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🚀 Feature request
Command
Description
By default, it is not required to explicitly type functions' return value, as TypeScript can always infer the type based on the function code.
But the inference assumes that the function's code is correct. If the code change or is incorrect, the return type inference will reflect that.
While it is acceptable in applications, as it will raise an error where the function is used if the types do not match anymore, it is more dangerous in a library, as the functions are not used directly. It can lead to unwanted breaking changes in the public API.
Describe the solution you'd like
TSLint has a rule to enforce functions' return type (and ESLint too, for the future migration):
It could be added when doing
ng g library
.It is an easy change as:
tslint.json
due to--prefix
optionThis is a really easy thing to add, I can take care of the PR if Angular team signal is green.
If it is considered a too big step, it could be added only if the workspace is already using TypeScript
strict
mode (following the new--strict
option in Angular >= 9). But I think as we are talking about libraries, it should really be the default: libraries authors must be stricter to avoid compatibility issues (strict
mode should even be the default for libraries, but it would be another discussion).The text was updated successfully, but these errors were encountered: