Skip to content

Commit dfa30bb

Browse files
authored
Ensure moduleType is structured during cloneTypeAsModuleType (#51136)
1 parent 074bf34 commit dfa30bb

File tree

5 files changed

+161
-2
lines changed

5 files changed

+161
-2
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5156,7 +5156,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
51565156
getPropertyOfType(type, InternalSymbolName.Default, /*skipObjectFunctionPropertyAugment*/ true) ||
51575157
isEsmCjsRef
51585158
) {
5159-
const moduleType = getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!, reference);
5159+
const moduleType = type.flags & TypeFlags.StructuredType
5160+
? getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!, reference)
5161+
: createDefaultPropertyWrapperForModule(symbol, symbol.parent);
51605162
return cloneTypeAsModuleType(symbol, moduleType, referenceParent);
51615163
}
51625164
}
@@ -34177,7 +34179,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3417734179
return createPromiseReturnType(node, anyType);
3417834180
}
3417934181

34180-
function createDefaultPropertyWrapperForModule(symbol: Symbol, originalSymbol: Symbol, anonymousSymbol?: Symbol | undefined) {
34182+
function createDefaultPropertyWrapperForModule(symbol: Symbol, originalSymbol: Symbol | undefined, anonymousSymbol?: Symbol | undefined) {
3418134183
const memberTable = createSymbolTable();
3418234184
const newSymbol = createSymbol(SymbolFlags.Alias, InternalSymbolName.Default);
3418334185
newSymbol.parent = originalSymbol;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// [tests/cases/compiler/moduleExportNonStructured.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "test",
6+
"version": "1.0.0",
7+
"description": "",
8+
"type": "module",
9+
"module": "index.mjs"
10+
}
11+
12+
//// [index.mts]
13+
import * as exportAny from "./exportAny.cjs";
14+
import * as exportUnknown from "./exportUnknown.cjs";
15+
import * as exportSymbol from "./exportSymbol.cjs";
16+
17+
import type * as exportAnyType from "./exportAny.cjs";
18+
import type * as exportUnknownType from "./exportUnknown.cjs";
19+
import type * as exportSymbolType from "./exportSymbol.cjs";
20+
21+
//// [exportAny.d.cts]
22+
declare const __: any;
23+
export = __;
24+
25+
26+
//// [exportUnknown.d.cts]
27+
declare const __: unknown;
28+
export = __;
29+
30+
31+
//// [exportSymbol.d.cts]
32+
declare const __: symbol;
33+
export = __;
34+
35+
36+
//// [index.mjs]
37+
export {};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
=== tests/cases/compiler/index.mts ===
2+
import * as exportAny from "./exportAny.cjs";
3+
>exportAny : Symbol(exportAny, Decl(index.mts, 0, 6))
4+
5+
import * as exportUnknown from "./exportUnknown.cjs";
6+
>exportUnknown : Symbol(exportUnknown, Decl(index.mts, 1, 6))
7+
8+
import * as exportSymbol from "./exportSymbol.cjs";
9+
>exportSymbol : Symbol(exportSymbol, Decl(index.mts, 2, 6))
10+
11+
import type * as exportAnyType from "./exportAny.cjs";
12+
>exportAnyType : Symbol(exportAnyType, Decl(index.mts, 4, 11))
13+
14+
import type * as exportUnknownType from "./exportUnknown.cjs";
15+
>exportUnknownType : Symbol(exportUnknownType, Decl(index.mts, 5, 11))
16+
17+
import type * as exportSymbolType from "./exportSymbol.cjs";
18+
>exportSymbolType : Symbol(exportSymbolType, Decl(index.mts, 6, 11))
19+
20+
=== tests/cases/compiler/exportAny.d.cts ===
21+
declare const __: any;
22+
>__ : Symbol(__, Decl(exportAny.d.cts, 0, 13))
23+
24+
export = __;
25+
>__ : Symbol(__, Decl(exportAny.d.cts, 0, 13))
26+
27+
28+
=== tests/cases/compiler/exportUnknown.d.cts ===
29+
declare const __: unknown;
30+
>__ : Symbol(__, Decl(exportUnknown.d.cts, 0, 13))
31+
32+
export = __;
33+
>__ : Symbol(__, Decl(exportUnknown.d.cts, 0, 13))
34+
35+
36+
=== tests/cases/compiler/exportSymbol.d.cts ===
37+
declare const __: symbol;
38+
>__ : Symbol(__, Decl(exportSymbol.d.cts, 0, 13))
39+
40+
export = __;
41+
>__ : Symbol(__, Decl(exportSymbol.d.cts, 0, 13))
42+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
=== tests/cases/compiler/index.mts ===
2+
import * as exportAny from "./exportAny.cjs";
3+
>exportAny : { default: any; }
4+
5+
import * as exportUnknown from "./exportUnknown.cjs";
6+
>exportUnknown : { default: unknown; }
7+
8+
import * as exportSymbol from "./exportSymbol.cjs";
9+
>exportSymbol : { default: symbol; }
10+
11+
import type * as exportAnyType from "./exportAny.cjs";
12+
>exportAnyType : { default: any; }
13+
14+
import type * as exportUnknownType from "./exportUnknown.cjs";
15+
>exportUnknownType : { default: unknown; }
16+
17+
import type * as exportSymbolType from "./exportSymbol.cjs";
18+
>exportSymbolType : { default: symbol; }
19+
20+
=== tests/cases/compiler/exportAny.d.cts ===
21+
declare const __: any;
22+
>__ : any
23+
24+
export = __;
25+
>__ : any
26+
27+
28+
=== tests/cases/compiler/exportUnknown.d.cts ===
29+
declare const __: unknown;
30+
>__ : unknown
31+
32+
export = __;
33+
>__ : unknown
34+
35+
36+
=== tests/cases/compiler/exportSymbol.d.cts ===
37+
declare const __: symbol;
38+
>__ : symbol
39+
40+
export = __;
41+
>__ : symbol
42+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// @target: ESNext
2+
// @module: ESNext
3+
// @moduleResolution: NodeNext
4+
// @strict: true
5+
6+
// @filename: package.json
7+
{
8+
"name": "test",
9+
"version": "1.0.0",
10+
"description": "",
11+
"type": "module",
12+
"module": "index.mjs"
13+
}
14+
15+
// @filename: index.mts
16+
import * as exportAny from "./exportAny.cjs";
17+
import * as exportUnknown from "./exportUnknown.cjs";
18+
import * as exportSymbol from "./exportSymbol.cjs";
19+
20+
import type * as exportAnyType from "./exportAny.cjs";
21+
import type * as exportUnknownType from "./exportUnknown.cjs";
22+
import type * as exportSymbolType from "./exportSymbol.cjs";
23+
24+
// @filename: exportAny.d.cts
25+
declare const __: any;
26+
export = __;
27+
28+
29+
// @filename: exportUnknown.d.cts
30+
declare const __: unknown;
31+
export = __;
32+
33+
34+
// @filename: exportSymbol.d.cts
35+
declare const __: symbol;
36+
export = __;

0 commit comments

Comments
 (0)