Skip to content

Conversation

@james-pre
Copy link

This PR implements importing JSON modules with const context, which has the same effect as an as const assertion.

This was discussed in microsoft/TypeScript#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 of the compiler written in Typescript 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 01:54
@james-pre
Copy link
Author

@microsoft-github-policy-service agree

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, JSON imports receive literal types (e.g., "string" instead of string) and readonly modifiers, equivalent to using as const in TypeScript.

Changes:

  • Added importJsonAsConst boolean compiler option (opt-in, defaults to false)
  • Modified type checker to apply const context to JSON file expressions when the option is enabled
  • Added comprehensive test cases demonstrating both default and const-enabled behavior

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
testdata/tests/cases/compiler/jsonLiteralTypes.ts Test case demonstrating JSON imports with importJsonAsConst: true producing literal types
testdata/tests/cases/compiler/jsonLiteralTypesDefault.ts Test case verifying default behavior without the option produces wide types
testdata/baselines/reference/tsc/commandLine/help-all.js Updated command-line help to include the new importJsonAsConst option
testdata/baselines/reference/compiler/jsonLiteralTypes.* Baseline files showing expected literal types when option is enabled
testdata/baselines/reference/compiler/jsonLiteralTypesDefault.* Baseline files showing expected wide types and errors in default behavior
internal/tsoptions/parsinghelpers.go Added parsing logic for the importJsonAsConst option
internal/tsoptions/declscompiler.go Added command-line option definition with appropriate flags
internal/diagnostics/extraDiagnosticMessages.json Added diagnostic message for the new option
internal/diagnostics/diagnostics_generated.go Generated diagnostic code (6933) for the option description
internal/core/compileroptions.go Added ImportJsonAsConst field to compiler options struct
internal/checker/checker.go Modified isConstContext to return true for JSON expressions when option is enabled, and updated type resolution to skip widening for JSON imports

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant