Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

export = does not always "resolve to a module entity" #221

Open
mindplay-dk opened this issue Apr 1, 2016 · 3 comments
Open

export = does not always "resolve to a module entity" #221

mindplay-dk opened this issue Apr 1, 2016 · 3 comments

Comments

@mindplay-dk
Copy link

Example module:

import setRootPElement = require("./set-root-p-element")
import enforcePElements = require("./formatters/html/enforce-p-elements")
import ensureSelectableContainers = require("./formatters/html/ensure-selectable-containers")
import inlineElementsMode = require("./inline-elements-mode")

  export = {
    setRootPElement,
    enforcePElements,
    ensureSelectableContainers,
    inlineElementsMode
  }

When I try to import this, I get a TS 2497 error at the import site saying this file "resolves to a non-module entity and cannot be imported using this construct".

Modify the code as follows - introduce an intermediary variable - and then it works:

import setRootPElement = require("./set-root-p-element")
import enforcePElements = require("./formatters/html/enforce-p-elements")
import ensureSelectableContainers = require("./formatters/html/ensure-selectable-containers")
import inlineElementsMode = require("./inline-elements-mode")

  var plugins = {
    setRootPElement,
    enforcePElements,
    ensureSelectableContainers,
    inlineElementsMode
  }

export = plugins

How come I have to "trick" it into generating a module in this case?

I often have this problem in various other cases with top-level exports, such as when I'm exporting a single top-level function.

Is this normal?

I'm using release 1.8.9.

@mindplay-dk
Copy link
Author

For some reason my "hack" doesn't even always work, such as when the top-level export is a function...

import observeDomChanges = require("../../dom-observer")
import Immutable = require("immutable")

export = function() {
    return function (scribe) {
        // ...
    };
  };

In this case I can't seem to get it to resolve as a module at all - the "hack" (introducing a name and exporting that) doesn't fix it:

import observeDomChanges = require("../../dom-observer")
import Immutable = require("immutable")

  function foo() {
    return function (scribe) {
        // ...
    };
  };

  export = foo

@mhegazy
Copy link
Contributor

mhegazy commented Apr 1, 2016

There is a subtle distinction between export = <identifier> and export = <expression>. Only the earlier exports all meaning of an identifier. In your case, the object literal is treated as an expression, and what you are exporting is only the value side of that expression.

See more about this issue in microsoft/TypeScript#4325

@mindplay-dk
Copy link
Author

Okay, I understand the difference between an identifier and an expression, but you say "Only the earlier exports all meaning of an identifier", are you talking about the very first example?

import setRootPElement = require("./set-root-p-element")
import enforcePElements = require("./formatters/html/enforce-p-elements")
import ensureSelectableContainers = require("./formatters/html/ensure-selectable-containers")
import inlineElementsMode = require("./inline-elements-mode")

  export = {
    setRootPElement,
    enforcePElements,
    ensureSelectableContainers,
    inlineElementsMode
  }

Because this doesn't export anything - it doesn't even appear to become a module?

I thought anything with an import or export statement became a module.

So is it the error message that's incorrect in the first case? "resolves to a non-module entity and cannot be imported using this construct" - sounds to me like the file didn't evaluate to a module entity at all?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants