-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
This is the restated #27293. CC @ryanelian
TypeScript Version: master (471bc64)
Search Terms: esModuleInterop allowSyntheticDefaultImports error undefined default import export
Code: In the b
subdirectory, compile with tsc -b .
and run with node index.js
.
// tsconfig.common.json
{
"compilerOptions": {
"composite": true,
"declaration": true,
"target": "es6",
"module": "commonjs",
"esModuleInterop": true
}
}
// a/tsconfig.json
{
"extends": "../tsconfig.common.json",
"files": [
"index.ts"
]
}
// a/index.ts
export const foo = 42;
// b/tsconfig.json
{
"extends": "../tsconfig.common.json",
"references": [
{ "path": "../a" }
],
"files": [
"index.ts"
]
}
// b/index.ts
// Actual: compile OK. Expected: compile error.
import A from "../a";
// Actual: runtime error.
console.log(A.foo);
Expected behavior: The generated a/index.d.ts
uses some new syntax to mark the module as an ES6 module, so allowSyntheticDefaultImports
does not apply to it and the default import in b/index.ts
is a compile error.
Actual behavior: allowSyntheticDefaultImports
applies to module a
, so at compile time, the default import is accepted and resolves to the entire module, but at runtime, A.foo
raises an error:
REDACTED/b/index.js:7
console.log(a_1.default.foo);
^
TypeError: Cannot read property 'foo' of undefined
Playground Link: N/A, multiple files
Related Issues: #27293
dragomirtitian, saoudrizwan, jdolle, fgallardograzio, innocenzi and 3 more
Metadata
Metadata
Assignees
Labels
In DiscussionNot yet reached consensusNot yet reached consensusSuggestionAn idea for TypeScriptAn idea for TypeScript