Esm imports of cjs files should unconditionally have a default patched on (#48062)

This commit is contained in:
Wesley Wigham
2022-03-02 08:53:31 -08:00
committed by GitHub
parent ed45f058df
commit ff1f7b15b7
35 changed files with 565 additions and 346 deletions

View File

@@ -3687,12 +3687,18 @@ namespace ts {
return cloneTypeAsModuleType(symbol, defaultOnlyType, referenceParent);
}
if (getESModuleInterop(compilerOptions)) {
const targetFile = moduleSymbol?.declarations?.find(isSourceFile);
const isEsmCjsRef = targetFile && isESMFormatImportImportingCommonjsFormatFile(getUsageModeForExpression(reference), targetFile.impliedNodeFormat);
if (getESModuleInterop(compilerOptions) || isEsmCjsRef) {
let sigs = getSignaturesOfStructuredType(type, SignatureKind.Call);
if (!sigs || !sigs.length) {
sigs = getSignaturesOfStructuredType(type, SignatureKind.Construct);
}
if ((sigs && sigs.length) || getPropertyOfType(type, InternalSymbolName.Default, /*skipObjectFunctionPropertyAugment*/ true)) {
if (
(sigs && sigs.length) ||
getPropertyOfType(type, InternalSymbolName.Default, /*skipObjectFunctionPropertyAugment*/ true) ||
isEsmCjsRef
) {
const moduleType = getTypeWithSyntheticDefaultImportType(type, symbol, moduleSymbol!, reference);
return cloneTypeAsModuleType(symbol, moduleType, referenceParent);
}