Skip to content

Commit cb622b4

Browse files
committed
Do not report esModuleInterop needed error for json imports
Fixes #25400
1 parent 50ef631 commit cb622b4

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,7 @@ namespace ts {
22682268
function resolveESModuleSymbol(moduleSymbol: Symbol | undefined, referencingLocation: Node, dontResolveAlias: boolean): Symbol | undefined {
22692269
const symbol = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias);
22702270
if (!dontResolveAlias && symbol) {
2271-
if (!(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable))) {
2271+
if (!(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable)) && !getDeclarationOfKind(symbol, SyntaxKind.SourceFile)) {
22722272
error(referencingLocation, Diagnostics.Module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct, symbolToString(moduleSymbol!));
22732273
return symbol;
22742274
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/compiler/requireOfJsonFileWithoutEsModuleInterop.ts] ////
2+
3+
//// [file1.ts]
4+
import * as test from "./test.json"
5+
6+
//// [test.json]
7+
{
8+
"a": true,
9+
"b": "hello"
10+
}
11+
12+
//// [out/test.json]
13+
{
14+
"a": true,
15+
"b": "hello"
16+
}
17+
//// [out/file1.js]
18+
"use strict";
19+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as test from "./test.json"
3+
>test : Symbol(test, Decl(file1.ts, 0, 6))
4+
5+
=== tests/cases/compiler/test.json ===
6+
{
7+
"a": true,
8+
>"a" : Symbol("a", Decl(test.json, 0, 1))
9+
10+
"b": "hello"
11+
>"b" : Symbol("b", Decl(test.json, 1, 14))
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/file1.ts ===
2+
import * as test from "./test.json"
3+
>test : { "a": boolean; "b": string; }
4+
5+
=== tests/cases/compiler/test.json ===
6+
{
7+
>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; }
8+
9+
"a": true,
10+
>"a" : boolean
11+
>true : true
12+
13+
"b": "hello"
14+
>"b" : string
15+
>"hello" : "hello"
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @module: commonjs
2+
// @moduleResolution: node
3+
// @target:es2017
4+
// @strict: true
5+
// @resolveJsonModule: true
6+
// @outdir: out/
7+
// @fullEmitPaths: true
8+
9+
// @Filename: file1.ts
10+
import * as test from "./test.json"
11+
12+
// @Filename: test.json
13+
{
14+
"a": true,
15+
"b": "hello"
16+
}

0 commit comments

Comments
 (0)