Skip to content

Ensure moduleType is structured during cloneTypeAsModuleType #51136

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 6 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions tests/baselines/reference/moduleExportNonStructured.js
Original file line number Diff line number Diff line change
@@ -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 {};
42 changes: 42 additions & 0 deletions tests/baselines/reference/moduleExportNonStructured.symbols
Original file line number Diff line number Diff line change
@@ -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))

42 changes: 42 additions & 0 deletions tests/baselines/reference/moduleExportNonStructured.types
Original file line number Diff line number Diff line change
@@ -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

36 changes: 36 additions & 0 deletions tests/cases/compiler/moduleExportNonStructured.ts
Original file line number Diff line number Diff line change
@@ -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 = __;