-
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
noImplicitAny on single file #8855
Comments
Individual file compiler flags aren't supported at the moment. Figuring out how different files compiled with different flags interact would be a problem. You probably want something like disable certain errors per file. There is an issue request for that somewhere (I'm on a train) 🌹 |
You could patch the existing CompilerHost or create your own that suppresses arbitrary diagnostics from arbitrary files, but I don't believe there's anything in-built. |
@basarat we're incrementally adopting TypeScript - a use-case I imagine is shared by many! In this case opt-in is preferable. We start off files as |
The issue here is that types could leak into the compilation context. there is no grantee that an implicit any has a local effect. and thus there are no guarantees about refactoring, find all refes, etc.. in general if it is a point-in-time, e.g. migrating to TS or from implicit anys to noImplicitAny, etc, i would recommend doing it all in one shot, or splitting the compilation into two chunks, each with a different flag, and moving files/modules from one chunk to the other. You can see a similar response for --strictNullChecks in #8405 (comment). |
This is really difficult for a large code base sadly :-( We did think about maintaining two graphs, one strict and one unstrict, however this means modules can't be shared between the two which makes it hard. I might have a crack at wrapping |
Does #8855 (comment) not work for you? Edit: i.e. |
Great, that works! See below. Now I am wondering how I could wrap/configure IDE plugins so they give they filter errors in the same way. Any ideas on how I might do that? Seems ambitious… // main.ts
// ts-node --project main.ts
// https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
import ts = require('typescript');
const options: ts.CompilerOptions = {
// TODO: Consume project config somehow
// project: `server/tsconfig.json`,
outDir: 'tmp',
// Strict only
noImplicitAny: true
};
const program = ts.createProgram([`main.ts`]);
const emitResult = program.emit();
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
// We only care about no implicit any errors on "strict files"
const strictFiles = [
'strict-file.ts'
].map(str => `${__dirname}/${str}`)
const errors = allDiagnostics
.filter(diagnostic => {
const isStrictFile = strictFiles.indexOf(diagnostic.file.fileName) !== -1;
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
const isImplicitAnyError = / implicitly has an 'any(\[\])?' type.$/.test(message);
return isStrictFile ? true : !isImplicitAnyError;
});
const errorStr = errors
.map(diagnostic => {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
return `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;
})
.join('\n');
if (errorStr) {
process.stderr.write(errorStr);
process.stderr.write('\n\r');
process.exit(1);
} |
How about doing it on a per-type basis See #18540. It avoids the need to create two graphs, and now the check becomes a property of the type. A potential suggestion is to use comments to indicate strictness. /* @strict */ At any point during compilation, the use of type implicitly converts to any or vice versa, throw a compiler error. |
Is it possible to enable noImplicitAny for a single file? This would help us as we are gradually migrating and, when a file is complete, we can begin to use
noImplicitAny
.The text was updated successfully, but these errors were encountered: