Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added constant JSON imports and importJsonAsConst
  • Loading branch information
james-pre committed Jan 18, 2026
commit c5b1deec5a1086618a9735cf35fe0ce9494794c4
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12515,6 +12515,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (!declaration.statements.length) {
return emptyObjectType;
}
if (compilerOptions.importJsonAsConst) {
return checkExpression(declaration.statements[0].expression);
}
return getWidenedType(getWidenedLiteralType(checkExpression(declaration.statements[0].expression)));
}
if (isAccessor(declaration)) {
Expand Down Expand Up @@ -41395,6 +41398,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function isConstContext(node: Expression): boolean {
const parent = node.parent;
if (compilerOptions.importJsonAsConst && parent.kind === SyntaxKind.ExpressionStatement && isSourceFile(parent.parent) && isJsonSourceFile(parent.parent)) {
return true;
}
return isAssertionExpression(parent) && isConstTypeReference(parent.type) ||
isJSDocTypeAssertion(parent) && isConstTypeReference(getJSDocTypeAssertionType(parent)) ||
isValidConstAssertionArgument(node) && isConstTypeVariable(getContextualType(node, ContextFlags.None)) ||
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,15 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
description: Diagnostics.Enable_importing_json_files,
defaultValueDescription: false,
},
{
name: "importJsonAsConst",
type: "boolean",
affectsSemanticDiagnostics: true,
affectsBuildInfo: true,
category: Diagnostics.Language_and_Environment,
description: Diagnostics.Import_JSON_files_as_const_assertions,
defaultValueDescription: false,
},
{
name: "allowArbitraryExtensions",
type: "boolean",
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6719,6 +6719,10 @@
"category": "Message",
"code": 6932
},
"Import JSON files as const assertions.": {
"category": "Message",
"code": 6933
},

"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7442,6 +7442,7 @@ export interface CompilerOptions {
/** @internal */ help?: boolean;
ignoreDeprecations?: string;
importHelpers?: boolean;
importJsonAsConst?: boolean;
/** @deprecated */
importsNotUsedAsValues?: ImportsNotUsedAsValues;
/** @internal */ init?: boolean;
Expand Down