5

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 :-)

Damien Sawyer
  • 5,323
  • 3
  • 44
  • 56
  • 1
    No, but it really isn't hard to write strict TypeScript - what problem are you having exactly? `noImplicitAny` is the easiest to fix (just paste `: any` or `: never` or `: unknown` where necessary) – Dai Sep 15 '20 at 03:14
  • Hi. This is the current issue, but it was just as much about learning what TS can do. https://stackoverflow.com/questions/63893394/string-cannot-be-used-to-index-type-t – Damien Sawyer Sep 15 '20 at 03:33
  • 1
    Does this answer your question? [Can I disable ECMAscript strict mode for specific functions?](https://stackoverflow.com/questions/6020178/can-i-disable-ecmascript-strict-mode-for-specific-functions) – Tân Sep 15 '20 at 03:40
  • 2
    @Tân ECMAScript Strict Mode is largely unrelated to this question: TypeScript's `tsc` has many "strict" options, and ECMAScript Strict Mode is only one of those. The OP is asking primarily about `noImplicitAny` which is completely unrelated to ECMAScript's Strict Mode. – Dai Sep 15 '20 at 03:56
  • I think the answers on that question explain the issue well. Understand it and don't try to work around it. Afaik, based on a few github issues found, there's currently no way of turning typescripts strict mode down for a specific file. –  Sep 15 '20 at 05:42

1 Answers1

5

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.

Tim Perry
  • 11,766
  • 1
  • 57
  • 85