Skip to content

Disallow export-assignment modules to be imported with import * as from ? #6809

@Arnavion

Description

@Arnavion

While import * as foo from "foo"; compiles to var foo = require("foo"); (and equivalent for AMD) and thus works as a way to use export-assignment modules, the ES6 spec does not define this behavior. Instead the spec requires that foo be an immutable namespace object that only contains the exports of the module as properties.

Do you think it's worth disallowing the use of import * as Foo from "./foo" for export-assignment modules?

For example, if foo.d.ts is

declare module "foo" {
    function foo();
    export = foo;
}

Option 1: The compiler only allows it to be imported with import foo = require("foo"); and errors otherwise. The error message could also suggest using import foo from "foo"; with --allowSyntheticDefaultImports and a suitable loader.

Option 2: The compiler allows the ES6-style import but strips the () member, so the imported foo cannot be used as a function but other properties on it are still available. This is closer to the ES6 spec but will probably cause more user confusion. It also doesn't prevent the case where the export is of any type.

It is a breaking change, but there is precedent with the other recent breaking change to make ES6 imports immutable - that one was also based on an ES6 spec interpretation of not-necessarily-ES6 modules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions