Can you turn down strict mode et al on a single file or code block?

Viewed 2903

I have strict mode turned on in my tsconfig.json. I'm having trouble bending some parts of my code to the will of the compiler. Instead of turning the setting off for the entire project, is there a way to mark specific files or - even better - blocks of code with reduced stringency?

So I'm thinking something like this.

function foo() {
 // implicitAny not allowed here 

/// noImplicitAny false

 // implicitAny allowed here 

/// noImplicitAny true
}

I thought that triple dash directives might help, but they don't appear to.

Thanks :-)

1 Answers

No, it's not currently possible to do this.

For context, see this TypeScript issue tracking it as a possible feature for the future: https://github.com/Microsoft/TypeScript/issues/28306

That issue also notes some workarounds that might work for you, such as creating a non-strict primary tsconfig file that includes your whole project, and then local strict config(s) which inherit from that and enable strict mode only for their own listed files. That's not per-file, and it's a bit of a pain to maintain, but it might help.

Otherwise, if you need to disable checks for a specific section of code, you can avoid almost all strict typing errors by explicitly setting the types of your parameters & variables to any everywhere. In general of course it's better to properly handle the issues though, since most strict mode errors are real errors that you should be aware of.

Related