Skip to content

Conversation

@james-pre
Copy link

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:

Development in this codebase is winding down and PRs will only be merged if they fix critical 6.0 issues

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 const assertion.

This was discussed in and fixes #32063. The issue suggests using a const import 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.importJsonAsConst to true in 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.

Copilot AI review requested due to automatic review settings January 18, 2026 03:24
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Jan 18, 2026
@typescript-bot typescript-bot added For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jan 18, 2026
@typescript-bot
Copy link
Collaborator

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
@typescript-bot
Copy link
Collaborator

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.

Copy link
Contributor

Copilot AI left a 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 importJsonAsConst boolean 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

@typescript-bot
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

import ConstJson from './config.json' as const;

2 participants