diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 552a64cf77cbb..da6bb3b16b0a3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5127,7 +5127,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { getPropertyOfType(type, InternalSymbolName.Default, /*skipObjectFunctionPropertyAugment*/ true) || isEsmCjsRef ) { - const moduleType = getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!, reference); + const moduleType = type.flags & TypeFlags.StructuredType + ? getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!, reference) + : createDefaultPropertyWrapperForModule(symbol, symbol.parent); return cloneTypeAsModuleType(symbol, moduleType, referenceParent); } } @@ -33974,7 +33976,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return createPromiseReturnType(node, anyType); } - function createDefaultPropertyWrapperForModule(symbol: Symbol, originalSymbol: Symbol, anonymousSymbol?: Symbol | undefined) { + function createDefaultPropertyWrapperForModule(symbol: Symbol, originalSymbol: Symbol | undefined, anonymousSymbol?: Symbol | undefined) { const memberTable = createSymbolTable(); const newSymbol = createSymbol(SymbolFlags.Alias, InternalSymbolName.Default); newSymbol.parent = originalSymbol; diff --git a/tests/baselines/reference/moduleExportNonStructured.js b/tests/baselines/reference/moduleExportNonStructured.js new file mode 100644 index 0000000000000..3593b95eb6989 --- /dev/null +++ b/tests/baselines/reference/moduleExportNonStructured.js @@ -0,0 +1,37 @@ +//// [tests/cases/compiler/moduleExportNonStructured.ts] //// + +//// [package.json] +{ + "name": "test", + "version": "1.0.0", + "description": "", + "type": "module", + "module": "index.mjs" +} + +//// [index.mts] +import * as exportAny from "./exportAny.cjs"; +import * as exportUnknown from "./exportUnknown.cjs"; +import * as exportSymbol from "./exportSymbol.cjs"; + +import type * as exportAnyType from "./exportAny.cjs"; +import type * as exportUnknownType from "./exportUnknown.cjs"; +import type * as exportSymbolType from "./exportSymbol.cjs"; + +//// [exportAny.d.cts] +declare const __: any; +export = __; + + +//// [exportUnknown.d.cts] +declare const __: unknown; +export = __; + + +//// [exportSymbol.d.cts] +declare const __: symbol; +export = __; + + +//// [index.mjs] +export {}; diff --git a/tests/baselines/reference/moduleExportNonStructured.symbols b/tests/baselines/reference/moduleExportNonStructured.symbols new file mode 100644 index 0000000000000..fb292beb97a69 --- /dev/null +++ b/tests/baselines/reference/moduleExportNonStructured.symbols @@ -0,0 +1,42 @@ +=== tests/cases/compiler/index.mts === +import * as exportAny from "./exportAny.cjs"; +>exportAny : Symbol(exportAny, Decl(index.mts, 0, 6)) + +import * as exportUnknown from "./exportUnknown.cjs"; +>exportUnknown : Symbol(exportUnknown, Decl(index.mts, 1, 6)) + +import * as exportSymbol from "./exportSymbol.cjs"; +>exportSymbol : Symbol(exportSymbol, Decl(index.mts, 2, 6)) + +import type * as exportAnyType from "./exportAny.cjs"; +>exportAnyType : Symbol(exportAnyType, Decl(index.mts, 4, 11)) + +import type * as exportUnknownType from "./exportUnknown.cjs"; +>exportUnknownType : Symbol(exportUnknownType, Decl(index.mts, 5, 11)) + +import type * as exportSymbolType from "./exportSymbol.cjs"; +>exportSymbolType : Symbol(exportSymbolType, Decl(index.mts, 6, 11)) + +=== tests/cases/compiler/exportAny.d.cts === +declare const __: any; +>__ : Symbol(__, Decl(exportAny.d.cts, 0, 13)) + +export = __; +>__ : Symbol(__, Decl(exportAny.d.cts, 0, 13)) + + +=== tests/cases/compiler/exportUnknown.d.cts === +declare const __: unknown; +>__ : Symbol(__, Decl(exportUnknown.d.cts, 0, 13)) + +export = __; +>__ : Symbol(__, Decl(exportUnknown.d.cts, 0, 13)) + + +=== tests/cases/compiler/exportSymbol.d.cts === +declare const __: symbol; +>__ : Symbol(__, Decl(exportSymbol.d.cts, 0, 13)) + +export = __; +>__ : Symbol(__, Decl(exportSymbol.d.cts, 0, 13)) + diff --git a/tests/baselines/reference/moduleExportNonStructured.types b/tests/baselines/reference/moduleExportNonStructured.types new file mode 100644 index 0000000000000..2b9bcc5340c12 --- /dev/null +++ b/tests/baselines/reference/moduleExportNonStructured.types @@ -0,0 +1,42 @@ +=== tests/cases/compiler/index.mts === +import * as exportAny from "./exportAny.cjs"; +>exportAny : { default: any; } + +import * as exportUnknown from "./exportUnknown.cjs"; +>exportUnknown : { default: unknown; } + +import * as exportSymbol from "./exportSymbol.cjs"; +>exportSymbol : { default: symbol; } + +import type * as exportAnyType from "./exportAny.cjs"; +>exportAnyType : { default: any; } + +import type * as exportUnknownType from "./exportUnknown.cjs"; +>exportUnknownType : { default: unknown; } + +import type * as exportSymbolType from "./exportSymbol.cjs"; +>exportSymbolType : { default: symbol; } + +=== tests/cases/compiler/exportAny.d.cts === +declare const __: any; +>__ : any + +export = __; +>__ : any + + +=== tests/cases/compiler/exportUnknown.d.cts === +declare const __: unknown; +>__ : unknown + +export = __; +>__ : unknown + + +=== tests/cases/compiler/exportSymbol.d.cts === +declare const __: symbol; +>__ : symbol + +export = __; +>__ : symbol + diff --git a/tests/cases/compiler/moduleExportNonStructured.ts b/tests/cases/compiler/moduleExportNonStructured.ts new file mode 100644 index 0000000000000..756f948570a86 --- /dev/null +++ b/tests/cases/compiler/moduleExportNonStructured.ts @@ -0,0 +1,36 @@ +// @target: ESNext +// @module: ESNext +// @moduleResolution: NodeNext +// @strict: true + +// @filename: package.json +{ + "name": "test", + "version": "1.0.0", + "description": "", + "type": "module", + "module": "index.mjs" +} + +// @filename: index.mts +import * as exportAny from "./exportAny.cjs"; +import * as exportUnknown from "./exportUnknown.cjs"; +import * as exportSymbol from "./exportSymbol.cjs"; + +import type * as exportAnyType from "./exportAny.cjs"; +import type * as exportUnknownType from "./exportUnknown.cjs"; +import type * as exportSymbolType from "./exportSymbol.cjs"; + +// @filename: exportAny.d.cts +declare const __: any; +export = __; + + +// @filename: exportUnknown.d.cts +declare const __: unknown; +export = __; + + +// @filename: exportSymbol.d.cts +declare const __: symbol; +export = __;