Skip to content

ES6 module declarations should be marked to exclude them from allowSyntheticDefaultImports #27329

@mattmccutchen

Description

@mattmccutchen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions