-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Support overriding type-checking compile flags on a per file basis #8405
Comments
We did talk about this suggestion before. specifically in the context of declaration files. the conclusion was that it is too subtle of a gesture to change the behavior of the type system. Would breaking the project into two pieces (e.g. some modules), and building each separately be an option? |
Ah, presumably in declaration files that was about changing the behavior of the type system at the point of declaration where it would have non-local effects. Here I'm looking to change the behavior at the point of use. If there was an exported interface declared in a file with a
Yes, although that is quite a lot of hassle compared to per-file or directory directives. An alternative that came to mind would be removing the |
This looks like a point-in-time migration request. once all |
I built something along those lines in #8855 (comment). However, the issue I have now is getting IDEs to provide corresponding feedback to developers. Presumably you would need to wrap the IDE plugin to do some filter, somehow? I opened microsoft/vscode#10337 to try and get more information on how to do this. |
Closure compiler has the per-file @Suppress {compilerCheck1|compilerCheck2|...} which is quite useful. Would be good to have this in typescript. |
Could you do something like:
In a loop until the whole project passes? Note that it's safe to check in strict null annotations on a project without SNC enabled. |
@evmar Migrating the project to strict null checks is the kind of a change that people will probably want to do from time to time, gradually when they have no other pressing tasks. If one person updates a couple of files to conform to strict null checking and switches the I think introducing the gradual strict null checking would take many older projects a long way towards the right direction and would be instrumental in some day switching the |
Adding my voice. We have a legacy project that we want to migrate slowly to strict checking. There is no way we can convert the whole project in one go. We really want to enable/disable the strict checks on individual files until the migration is complete for the same reason TKharaishvili mentioned: not everyone is going to embrace this cycle of turning on and off strict checking and may inadvertently add code that violates strict code on files that have already been migrated. |
Our solution was to have two TypeScript configs:
Workflow looks like this:
In IDE, we're using the standard For WebPack, we created a new config file |
+1 Definitely useful since our project has a legacy folder that we don't want to spend time fixing, but had to disable strict null checking in the entire project because of it. |
Would love to see this as well. With the
@mhegazy this is still better than the alternative. If it's between no checking vs checking that might miss some cases, I'll take the latter. In the meantime, I think I'm going to poke around instead with having two ESLint configurations, one that is applied to all files and one that only applies to files that are changed. At least that way we can enforce type signatures in changed code using typescript-eslint. |
@mockdeep Not to distract from the larger issue, but one technique I've used (not for exactly this problem, but hopefully you get the idea) is to do one up-front pass that inserts |
@evmar thanks for the tip. I think a variation on that I might try is to set them to |
Interesting article on how Figma did this on a large codebase: https://www.figma.com/blog/inside-figma-a-case-study-on-strict-null-checks/ tldr; |
I know I'm quite late to this conversation, but in case new folks are stumbling onto this issue and are interested in a workaround: https://github.com/allegro/typescript-strict-plugin seems to be a plugin that addresses this issue head on. |
I would like to make use of the
--strictNullChecks
option in an existing TypeScript codebase that is ~25K LOC in size. Not surprisingly, this throws up a substantial number of errors (~640).It would be very helpful if it was possible to tackle them gradually and keep the codebase compilable the whole time. The need to be able to tackle these issues gradually is especially true for
--strictNullChecks
because a reasonable number of the errors that came up are real potential issues that require some thought, rather than problems which can just be fixed in a mechanical fashion.Ideally what I would like is to be able to suppress checks on a per-file basis, enabling a gradual conversion as follows:
--strictNullChecks
for the project// @typescript:-strictNullChecks
to each file that produces an errorAlthough it is possible to suppress errors with the
!
operator, this could end up hiding real bugs and it would be unclear when the operator was used in the process of migration vs. to suppress an error in a case where it isn't possible to statically prove to the compiler that a value is non-null.The text was updated successfully, but these errors were encountered: