-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Add support for importing JSON modules "as const" #63008
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
base: main
Are you sure you want to change the base?
Conversation
|
The TypeScript team hasn't accepted the linked issue #32063. If you can get it accepted, this PR will have a better chance of being reviewed. |
1 similar comment
|
The TypeScript team hasn't accepted the linked issue #32063. If you can get it accepted, this PR will have a better chance of being reviewed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for importing JSON modules with const assertion semantics through a new importJsonAsConst compiler option. When enabled, imported JSON modules retain literal types (e.g., "string" instead of string) and become readonly, equivalent to applying as const to the JSON object.
Changes:
- Added new
importJsonAsConstboolean compiler option - Modified type checker to treat JSON expressions as const contexts when option is enabled
- Added comprehensive tests verifying both enabled and disabled behavior
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
src/compiler/types.ts |
Adds importJsonAsConst boolean property to CompilerOptions interface |
src/compiler/diagnosticMessages.json |
Adds user-facing description message for the new compiler option |
src/compiler/commandLineParser.ts |
Registers importJsonAsConst as a command-line option with appropriate flags and metadata |
src/compiler/checker.ts |
Implements feature by (1) skipping type widening for JSON modules when option is enabled, and (2) treating JSON expressions as const contexts |
tests/cases/compiler/jsonLiteralTypes.ts |
Tests that JSON imports have literal types when importJsonAsConst: true |
tests/cases/compiler/jsonLiteralTypesDefault.ts |
Tests that JSON imports have wide types when option is disabled (default behavior) |
tests/baselines/reference/jsonLiteralTypes.* |
Baseline outputs verifying literal types with option enabled |
tests/baselines/reference/jsonLiteralTypesDefault.* |
Baseline outputs verifying wide types and expected errors without option |
|
Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page. Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up. |
This is the same feature as microsoft/typescript-go#2539, implemented for the "legacy" compiler (i.e. the one written in TS). I'm aware of the current status of feature PRs:
I feel that this is a relatively small feature and that the feature parity between v6 and v7 would be really helpful, especially consider the clear demand for it (1313 thumbs up on the issue at the time of writing)
This PR implements importing JSON modules with const context, which has the same effect as an
as constassertion.This was discussed in and fixes #32063. The issue suggests using a
constimport attribute, however this PR uses a compiler option which is much simpler and should be more maintainable.This feature is opt-in, so no existing code will be broken. It can be enabled by setting
compilerOptions.importJsonAsConsttotruein tsconfig.I've already tested a patch on some of my own code and it works great. I also added some new tests for completeness.