diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4ddbaef0e1..224820e9c6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2268,7 +2268,7 @@ namespace ts { function resolveESModuleSymbol(moduleSymbol: Symbol | undefined, referencingLocation: Node, dontResolveAlias: boolean): Symbol | undefined { const symbol = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias); if (!dontResolveAlias && symbol) { - if (!(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable))) { + if (!(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable)) && !getDeclarationOfKind(symbol, SyntaxKind.SourceFile)) { error(referencingLocation, Diagnostics.Module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct, symbolToString(moduleSymbol!)); return symbol; } diff --git a/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.js b/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.js new file mode 100644 index 0000000000..ea5ac25b1b --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.js @@ -0,0 +1,19 @@ +//// [tests/cases/compiler/requireOfJsonFileWithoutEsModuleInterop.ts] //// + +//// [file1.ts] +import * as test from "./test.json" + +//// [test.json] +{ + "a": true, + "b": "hello" +} + +//// [out/test.json] +{ + "a": true, + "b": "hello" +} +//// [out/file1.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.symbols b/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.symbols new file mode 100644 index 0000000000..f84e06ec8f --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/file1.ts === +import * as test from "./test.json" +>test : Symbol(test, Decl(file1.ts, 0, 6)) + +=== tests/cases/compiler/test.json === +{ + "a": true, +>"a" : Symbol("a", Decl(test.json, 0, 1)) + + "b": "hello" +>"b" : Symbol("b", Decl(test.json, 1, 14)) +} diff --git a/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.types b/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.types new file mode 100644 index 0000000000..3bd9e1d98b --- /dev/null +++ b/tests/baselines/reference/requireOfJsonFileWithoutEsModuleInterop.types @@ -0,0 +1,16 @@ +=== tests/cases/compiler/file1.ts === +import * as test from "./test.json" +>test : { "a": boolean; "b": string; } + +=== tests/cases/compiler/test.json === +{ +>{ "a": true, "b": "hello"} : { "a": boolean; "b": string; } + + "a": true, +>"a" : boolean +>true : true + + "b": "hello" +>"b" : string +>"hello" : "hello" +} diff --git a/tests/cases/compiler/requireOfJsonFileWithoutEsModuleInterop.ts b/tests/cases/compiler/requireOfJsonFileWithoutEsModuleInterop.ts new file mode 100644 index 0000000000..d3020c553a --- /dev/null +++ b/tests/cases/compiler/requireOfJsonFileWithoutEsModuleInterop.ts @@ -0,0 +1,16 @@ +// @module: commonjs +// @moduleResolution: node +// @target:es2017 +// @strict: true +// @resolveJsonModule: true +// @outdir: out/ +// @fullEmitPaths: true + +// @Filename: file1.ts +import * as test from "./test.json" + +// @Filename: test.json +{ + "a": true, + "b": "hello" +} \ No newline at end of file