Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you enable TypeScript strict mode on a per file?

Tags:

typescript

Is it possible to enable strict checks done by the TSC compiler (i.e. --strict et al.) file-by-file, say using a special comment? I'm working on an established Angular codebase that mostly doesn't take advantage of TS, and I'd like to have at least my own code be neater without also having to fix everybody else's code by turning the flag on for the whole project.

like image 752
millimoose Avatar asked Nov 14 '18 03:11

millimoose


People also ask

How do you set TypeScript to strict?

You can either set the --strict flag on the command line or specify the strict option within your project's tsconfig. json file to opt into this mode. As of TypeScript 4.3 in August 2021, the --strict flag enables the following eight compiler options: --alwaysStrict.

Is TypeScript strict by default?

Because TypeScript was developed primarily as superset of JavaScript, by default it allows us to write code that is not super strict in types and other aspects.

How do I disable TypeScript strict mode?

You can do that by compiling with the --noImplicitUseStrict compiler option—add "noImplicitUseStrict": true to "compilerOptions" in tsconfig. json. Doing so will prevent the compiler from emitting "use strict" .


1 Answers

Not currently. See this open suggestion; I don't expect the TypeScript team to get around to it any time soon. One workaround might be to write a script that runs tsc with the stricter checks enabled and filters the output for the files you want; see this answer for some prior art.

like image 143
Matt McCutchen Avatar answered Sep 19 '22 05:09

Matt McCutchen