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
Allow some per file compiler options #49886
base: main
Are you sure you want to change the base?
Conversation
@typescript-bot pack this |
Heya @weswigham, I've started to run the tarball bundle task on this PR at 459196b. You can monitor the build here. |
@typescript-bot pack this now that baselines should be green again :) |
Heya @weswigham, I've started to run the tarball bundle task on this PR at 97bba40. You can monitor the build here. |
Is this opt-in only, or will it be possible to opt-out as well? |
You should be able to write things like |
@typescript-bot pack this |
Heya @weswigham, I've started to run the tarball bundle task on this PR at ef3813c. You can monitor the build here. |
Hey @weswigham, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
In this PR, we allow certain compiler options to be configured via comments within a file for the scope of that file. This means you can write, for example:

Fixes #31035
I've wanted to look into this since I originally added the generic pragma-parsing infrastructure for
@ts-check
and related comments.To start, I'm aiming to allow every compiler option in the "Strictness" category as a per-file configurable option. That way you can toss a
// @ts-strict
into a (potentially javascript) file and get strict mode checking in it. Currently in this PR, every option in that category is supported except forstrictNullChecks
andexactOptionalPropertyTypes
- they do some global type-identity configuration that's taking me a bit longer to abstract away into a per-file configuration. (AlsostrictNullChecks
just straight up is referenced more than any other compiler option.)To start off the discussion, in the linked issue, @RyanCavanaugh asked:
Currently in this PR, whichever is stricter. If either file the types originated from has it set, it's related strictly. Could go the other way and relate loosely if either is explicitly non-strict if looser checking is more preferable. Honestly, most of these "which context controls the flag" already have fairly canonical answers, either because we already look up a context to check JS-y-ness (for JS-file-specific-behaviors or error ignoring), or to issue an error in (in which case the error node location is a pretty logical context).
Anyways, I'm hoping this implementation can jumpstart discussion on the value of in-file pragmas for compiler options, specifically with the goal of bikeshedding the syntax and what options we should ultimately expose on a per-file basis. (Technically, I've tried to make the internal APIs for adding new per-file pragmas very type-safe and very easy to use.)
TODO list:
strictNullChecks
(Per file option: strictNullChecks #50347 for separate review comments, since it's big)exactOptionalPropertyTypes